C问题:没有警告?
main() { printf("Hello World."); }
为什么在gcc编译器中没有产生警告,即使我们声明main()返回类型为’int’
因为你没有使用-Wall标志。 当你这样做时,你应该得到:
foo.c:1: warning: return type defaults to 'int' foo.c: In function 'main': foo.c:1: warning: implicit declaration of function 'printf' foo.c:1: warning: incompatible implicit declaration of built-in function 'printf' foo.c:1: warning: control reaches end of non-void function
您是否忘记编译并启用警告:
gcc -Wall ...
你的主要function什么都没有。 所以在void main()中修改。 通常是:
int main() { printf("Hello world"); return 0; }
没有警告,因为这是合法的ANSI C89。 隐式假定没有指定返回类型的函数返回int
。
如果要编译为C89,但要注意使用隐式int,则应将-Wimplicit-int
作为命令行参数传递(或者-Wall
,它会启用该警告,以及其他一些警告)。
如果要编译为C99,则应传递-std=c99
和-pedantic-errors
,如果使用隐式int,将导致编译器发出错误。