#C #C++ #Programming Languages
- In C, the int main() or int main(void) function is declared to return an int. Hence, the function must return an integer value to indicate the program's exit status.
- Writing return 0; explicitly tells the operating system that the program has been completed successfully.
- If you omit it, especially in C89/C99, you're not following the standard strictly, which may lead to undefined behavior.
- Although some compilers might add an implicit return 0; at the end, it's best to include it explicitly for clarity and portability.
- In C++11, if control reaches the end of main() without encountering a return statement, the compiler automatically adds return 0;. This means you don't strictly have to write it, and the program will still indicate success.
- Despite the implicit return, many programmers still include return 0; for clarity and to maintain consistency with older C++ codebases.