Program compiles with and without return 0 [duplicate]
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 callingexit
with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executingreturn 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.
精彩评论