Possible Duplicate:
What is the proper declaration of main?
Without citing any code in particular, I am looking for an explanation of the below example:
#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 0;
}
I don’t understand what return 0 does. Can you please explain this in as plain English as possible?
This defines the exit status of the process. Despite being an
int, on Unix-like systems, the value is always in the range 0-255 (see Exit and Exit Status). On Microsoft systems you may use 32-bit signed integers as exit codes, which you can check with%ERRORLEVEL%. For portability, I’d recommend sticking to the 0-255 range.Here is a trivial example:
Build:
Run (in bash):
Check the exit status:
Conventionally, a status of zero signifies success and non-zero failure. This can be useful in shell scripts, and so forth to indicate the level of failure, if any:
Following the comments below…
In the standard C++ header
<cstdlib>, the following macros are defined:However, the Exit Status section of the GNU C Library documentation, describing the same macros, sagely states: