On TutorialsPoint.com, exit is passed the value 0
, while people often pass it 1
. I've even seen exit(3);
What do the different values mean?
The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system. The general form of the exit() function is as follows − void exit (int code);
exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been executed without any error or interrupt.
In C++, what is the difference between exit(0) and return 0 ? When exit(0) is used to exit from program, destructors for locally scoped non-static objects are not called. But destructors are called if return 0 is used.
return returns from the current function; it's a language keyword like for or break . exit() terminates the whole program, wherever you call it from.
By convention, a program that exits successfully calls exit
(or returns from main
) with a value of 0. Shell programs (most programs, actually) will look for this to determine if a program ran successfully or not.
Any other value is considered an abnormal exit. What each of those values mean is defined by the program in question.
On Unix and similar systems, only the lower 8 bits of the exit value are used as the exit code of the program and are returned to the parent process on a call to wait
. Calling exit(n)
is equivalent to calling exit(n & 0xff)
From the man page:
The
exit()
function causes normal process termination and the value ofstatus & 0377
is returned to the parent (seewait
(2)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With