[英]gcc/g++ are giving different response to empty main function
I am using g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 我正在使用g ++(Ubuntu / Linaro 4.6.1-9ubuntu3)4.6.1 gcc(Ubuntu / Linaro 4.6.1-9ubuntu3)4.6.1
If I make a cpp and c file that contains only 如果我制作一个仅包含的cpp和c文件
int main(const int argc, const char *const argv[])
{
}
and compile it with g++ -Wall test_warnings.cpp
I get no warning. 并使用
g++ -Wall test_warnings.cpp
编译我没有得到任何警告。
If I compile it with gcc -Wall test_warnings.c
I get the warning you would expect: 如果我使用
gcc -Wall test_warnings.c
编译,则会收到警告,提示您:
test_warnings.c: In function ‘main’:
test_warnings.c:4:1: warning: control reaches end of non-void function [-Wreturn-type]
The same behavior is exhibited if -Wreturn-type is used instead of -Wall. 如果使用-Wreturn-type而不是-Wall,则会表现出相同的行为。
Why isn't g++ giving me a warning that the return is missing? 为什么g ++不会警告我缺少返回值?
Because C and C++ are different languages. 因为C和C ++是不同的语言。
In C++, reaching the end of main()
without executing a return
statement is equivalent to executing return 0;
在C ++中,不执行
return
语句而到达main()
的末尾等效于执行return 0;
. 。
In C, as of the 1990 ISO standard, falling off the end of main()
returns an undefined status to the calling environment. 从1990年ISO标准开始,在C语言中,脱离
main()
的末尾将向调用环境返回未定义的状态。
C99 changed this, essentially adopting the C++ rule -- but gcc doesn't implement C99 by default. C99改变了这一点,本质上采用了C ++规则-但gcc默认情况下不实现C99。 (Try compiling with
-std=c99
.) (尝试使用
-std=c99
编译。)
In any case, it can't hurt to add a return 0;
在任何情况下,添加
return 0;
都不会有问题return 0;
statement to the end of main()
. 声明到
main()
的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.