プログラミング言語C/C++において、main関数のreturn文を省略した場合の振る舞いに関するメモ。
まとめ:
C89/C90/C95
ANSI X3.159-1989(C89), ISO/IEC 9899:1990(C90), ISO/IEC 9899:1990/Amd.1:1995(C95)。
If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined.
C99/C11
C99 5.1.2.2.3/p1より引用(下線部は強調)。C11でも同一。
If the return type of the
main
function is a type compatible withint
, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
function as its argument; reaching the}
that terminates themain
function returns a value of 0. If the return type is not compatible withint
, the termination status returned to the host environment is unspecified.
C++
C++03 3.6.1/p5より引用(下線部は強調)。C++11でも同一。
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 ofmain
without encountering areturn
statement, the effect is that of executingreturn 0;
関連URL
*1:gccはこの言語仕様に "厳格に" 従うらしい。c - GCC default main return value is not zero - Stack Overflow参照