The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;.
Flowing off the end of a value-returning function, except the main function and specific coroutines(since C++20), without a return statement is undefined behavior.
int main() {
// do something
} // ←ここで暗黙に return 0;
でも、同じことを他の関数でやると未定義の動作となります。
3. return 0; の役割 ― OSへの正常な終了の報告
main が返した整数は、そのままプロセスの終了コードとして OS に渡されます。cppreference (C 版の main の項) もこう書いています。
If the return statement is used, the return value is used as the argument to the implicit call to exit() (see below for details). The values zero and EXIT_SUCCESS indicate successful termination, the value EXIT_FAILURE indicates unsuccessful termination.
コメント