开发者

Program compiles with and without return 0 [duplicate]

开发者 https://www.devze.com 2023-01-09 22:41 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: What should main() return in C/C++?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What should main() return in C/C++?

This is a pretty basic question, I guess.

I've been programming for a year now but a friend of mine surprised me with a rather stupefying question.

Programs that start with 'int main()' on C++ seem to compile perfectly even with 'return 0;' removed and not replaced by any other return statement. And without a return statement at all, the program still shows that 'Process returned 0'.

Does this have any explanati开发者_Go百科on? Sorry if my question is silly!


§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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号