关于参数类型,这里已经有正确的答案,但是如果您想从编译器中听到它,则可以尝试添加一些标志(无论如何,标志几乎总是一个好主意)。
使用gcc foo.c -Wextra
我编译您的程序:
foo.c: In function ‘func’:
foo.c:5:5: warning: type of ‘param’ defaults to ‘int’ [-Wmissing-parameter-type]
奇怪的-Wextra
是,clang
它并没有抓住它(它-Wmissing-parameter-type
由于某种原因而无法识别,也许是因为上述历史原因),但是却-pedantic
做到了:
foo.c:5:10: warning: parameter 'param' was not declared,
defaulting to type 'int' [-pedantic]
int func(param)
^
1 warning generated.
对于原型问题,上面再说一遍,int func()
是指任意参数,除非您明确定义它,否则int func(void)
将给您带来预期的错误:
foo.c: In function ‘func’:
foo.c:6:1: error: number of arguments doesn’t match prototype
foo.c:3:5: error: prototype declaration
foo.c: In function ‘main’:
foo.c:12:5: error: too many arguments to function ‘func’
foo.c:5:5: note: declared here
或clang
作为:
foo.c:5:5: error: conflicting types for 'func'
int func(param)
^
foo.c:3:5: note: previous declaration is here
int func(void);
^
foo.c:12:20: error: too many arguments to function call, expected 0, have 1
int bla = func(10);
~~~~ ^~
foo.c:3:1: note: 'func' declared here
int func(void);
^
2 errors generated.