In the main function of a C++ program, what does `return 0` do and mean? [duplicate]
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:
$ cat -n exit_code.cpp
1 int main()
2 {
3 return 42;
4 }
5
Build:
$ make exit_code
g++ exit_code.cpp -o exit_code
Run (in bash):
$ ./exit_code
Check the exit status:
$ echo $?
42
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:
$ ./exit_code
exit_status=$?
if [[ ${exit_status} ]] ; then
echo "The process failed with status ${exit_status}."
else
echo "Success!"
fi
The process failed with status 42.
Following the comments below...
In the standard C++ header <cstdlib>
, the following macros are defined:
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
However, the Exit Status section of the GNU C Library documentation, describing the same macros, sagely states:
Portability note: Some non-POSIX systems use different conventions for exit status values. For greater portability, you can use the macros EXIT_SUCCESS and EXIT_FAILURE for the conventional status value for success and failure, respectively. They are declared in the file stdlib.h.
General return
ing
Every function has a return type.
In the below example, the type is void
, which is an "incomplete type" with no values; using this as the return type means that the function returns no value:
void foo() {
std::cout << "Hello world\n";
}
However, in the below example, the return type is int
:
int foo() {
return 3;
}
The return
statement determines what value calls to function foo
will evaluate to. So, std::cout << foo()
will result in "3
" being printed to standard output.
return
ing from main
, specifically
When the function in question happens to be the "main" function, or the program's entrypoint, it's a bit more special, because the "return value" of the "main" function is taken to be the program's "exit code" — it tells the calling environment (e.g. terminal session) whether the program's execution was deemed to be successful. It must be an int
, and a value of 0
here means "everything went fine":
It's worth noting that you can actually completely omit return 0;
in the "main" function as it will be included implicitly. This doesn't help you much, though, if you want to return 1;
or some other value, and it doesn't come into play with other functions.
Citations
[C++11: 3.6.1/5]:
A return statement inmain
has the effect of leaving the main function (destroying any objects with automatic storage duration) and callingstd::exit
with the return value as the argument. If control reaches the end ofmain
without encountering a return statement, the effect is that of executingreturn 0;
[C++11: 18.5/8]:
[[noreturn]] void exit(int status)
The function
exit()
has additional behavior in this International Standard:
- First, objects with thread storage duration and associated with the current thread are destroyed.
Next, objects with static storage duration are destroyed and functions registered by callingatexit
are called. See 3.6.3 for the order of destructions and calls. (Automatic objects are not destroyed as a result of calling exit().)
If control leaves a registered function called by exit because the function does not provide a handler for a thrown exception,terminate()
shall be called (15.5.1).- Next, all open C streams (as mediated by the function signatures declared in
<cstdio>
) with unwritten buffered data are flushed, all open C streams are closed, and all files created by callingtmpfile()
are removed.- Finally, control is returned to the host environment. If status is zero or
EXIT_SUCCESS
, an implementation-defined form of the status successful termination is returned. If status isEXIT_FAILURE
, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
Bootnote
I suggest one of these resources, as this sort of thing is explained properly in any decent peer-reviewed C++ book; YouTube tutorials are not a good way to learn C++, and Stack Overflow contributors will generally expect you to have a decent book to form your prior research before resorting to asking for help.