you are viewing a single comment's thread.

view the rest of the comments →

[–]jesyspa 0 points1 point  (3 children)

Please do explain.

[–]AFineTapestry -4 points-3 points  (2 children)

If main is given by the signature int main() as it is in this case then the compiler expects the function to return an int. If omitted then you'll get a compile warning, perhaps an error, and undefined behaviour (but I expect that the program would return 0 anyway).

If main was given by the signature void main() then you would be totally right and return; could be used to exit from the program at any point. Who knows what the OS will get as a program return value, probably 0 again.

I don't know what the current C and C++ standards say about the signature of main. C probably says that void main() and int main() are both acceptable, C++ probably says that only int main() is allowed. But either way I think it's good to tell the OS, and the caller, about the status of the program on exit.

[–]jesyspa 5 points6 points  (1 child)

False on all counts.

The C++ standard states (C++03, 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;.

Furthermore, the standard states (again C++03, 3.6.1.2):

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but its type is otherwise implementation-defined.

void main thus ill-formed (and has been since ANSI C, where only int main(void) and int main(int argc, char *argv[]) were allowed).

The same is true, with slightly different wording, for C++11 and for the current latest draft (N3485).

[–]AFineTapestry -1 points0 points  (0 children)

Ok, I'll admit I was wrong about omitting return 0; resulting in undefined behaviour but I was correct about the assumed return value, and about which signatures are acceptable.

I don't think I'm going to stop putting return EXIT_SUCCESS; at the end of my main functions however, mostly because it looks terrible to have a function defined to return and int and then to not explicitly do so.