Why does the main function work with no return value?

Normally it is not allowed for the control flow to reach the end of a non-void function without returning something. The main function is handled differently, as specified in the standard.

From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2960.pdf:

ยง 3.6.1/5

If control reaches the end of main without encountering a return
statement, the effect is that of executing return 0;

As for the rationale, I’m not sure, honestly. If someone knows, please add it to my answer or as a comment.

Leave a Comment