The usual way for a program to terminate is simply for its main
function to return. The exit status value returned from the
main
function is used to report information back to the process's
parent process or shell.
A program can also terminate normally by calling the exit
function (defined in stdlib.h
).
When a program exits, it can return to the parent process a small
amount of information about the cause of termination, using the
exit status. This is a value between 0 and 255 that the exiting
process passes as an argument to exit
.
There are conventions for what sorts of status values certain programs should return. The most common convention is simply 0 for success and 1 for failure. Programs that perform comparison use a different convention: they use status 1 to indicate a mismatch, and status 2 to indicate an inability to compare. Your program should follow an existing convention if an existing convention makes sense for it.
Portability note: 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
.
Program Termination (from the GNU C Library Reference Manual)
Maintained by John Loomis, last updated 4 September 2006