Must the int main() function return a value in all compilers? [duplicate]

Why is it not necessary to include the return statement while using int main() in some compilers for C++? What about Turbo C++?


In C++, and in C99 and C11, it is a special rule of the language that if the control flow reaches the end of the main function, then the function impliclty returns 0.


In C++ and C99/C11, without a return statement in main function, it's default to return 0;

§ 3.6.1 Main function

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::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;

also read wiki page C/C++ main function

In case a return value is not defined by the programmer, an implicit return 0; at the end of the main() function is inserted by the compiler; this behavior is required by the C++ standard.


main must return an int, some compilers, including Turbo C++, may allow other return values, notably void main, but it's wrong, never use that.

However in C++, if you don't explicitly return a value in main, it's the same as return 0;

C++11 §3.6.1 Main function section 5

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::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;

Note that for C, this is only supported in C99 and later, but not supported by C89.