1

可能重复:
main() 在 C/C++ 中应该返回什么?

这是一个非常基本的问题,我猜。

我已经编程一年了,但我的一个朋友用一个相当令人震惊的问题让我感到惊讶。

在 C++ 上以 'int main()' 开头的程序似乎可以完美编译,即使使用 'return 0;' 已删除且未被任何其他返回语句替换。并且根本没有返回语句,程序仍然显示“进程返回 0”。

这有什么解释吗?对不起,如果我的问题很愚蠢!

4

2 回答 2

5

§3.6.1/5:

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

于 2010-07-31T21:29:23.573 回答
1

From the accepted answer of What should main() return in C/C++?

It's also worth noting that in C++, int main() can be left without a return value at which point it defaults to returning 0. This is also true with a C99 program. Whether return 0 should be omitted or not is open to debate.

于 2010-07-31T21:28:00.907 回答