What is the correct value to return to the operating system upon the successful completion of a program?
开发者_高级运维What is the correct value to return to the operating system upon the successful completion of a program
return EXIT_SUCCESS;
from your main()
function.
Alternatively, std::exit(EXIT_SUCCESS)
from just about anywhere in your program (it gets more complex if you have multiple threads/processes, obviously).
Note that on most platforms (and, in particular, POSIX), EXIT_SUCCESS
has a value of 0
. So return 0
or exit(0)
would usually work just as well.
Your main()
function should return 0
on success.
If you call exit()
, you can call it with either 0
or the macro EXIT_SUCCESS
. From the FDIS, 18.5.8, concerning void exit(int status)
:
If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.
To explain returning zero from main
, here is 3.6.1.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;
.
精彩评论