The return type of main()
Association of C & C++ Users
64 Southfield Road
Oxford, OX4 1PA
A proposal to make explicit that the return type of main is int.
I propose that paragraph 1 of 3.6.1 (basic.start.main) be changed by
'with a return type of int' after '... called main'
As detailed in the following discussion clause, this return type is
already implicit in the text of the WP but the proliferation of
compilers supporting a void return type for main suggests that this is
not clear. It is also possible that such an implicit requirement might
be lost at some stage of future editing. Making the requirement
explicit protects against accidental change.
Paragraph 5 of 3.6.1 states that the return value of main() is passed
to exit() as the argument for the int parameter of exit(). Furthermore
this paragraph adds a special condition to main() designating that
reaching the end of main() without a return statement is equivalent to
return 0. The motive for the special rule for 'falling out' of main()
is precisiely to avoid undefined behaviour at the point of exit from a
program. Clearly the return type of main() must be convertable to an
int value. That being the case, it would be perverse to allow main to
return anything other than an int value.
(Note that the prototype of exit is somewhat eccentric as it returns a
value to the environment (explicitly the value passed to it as its
argument) though its return type is void. Perhaps this is because
exit() cannot return to the program.)