Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between exit(0) and exit(1) in C?

Tags:

c

People also ask

Why do we use exit 0?

Definition of exit(0) It is used to terminate the program or let the control exit out of the program. It reports the operating system about the successful termination of the program which indicates to the operating system that the task of the program has been successfully completed.

What is the difference between Exit 0 and return 0?

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.

What is the difference between exit () and return () in C?

return is a statement that returns the control of the flow of execution to the function which is calling. Exit statement terminates the program at the point it is used.


What is the difference between exit(0) and exit(1) in C language?

exit(0) indicates successful program termination & it is fully portable, While
exit(1) (usually) indicates unsucessful termination. However, it's usage is non-portable.

Note that the C standard defines EXIT_SUCCESS and EXIT_FAILURE to return termination status from a C program.

0 and EXIT_SUCCESS are the values specified by the standard to indicate successful termination, however, only EXIT_FAILURE is the standard value for returning unsucessful termination. 1 is used for the same in many implementations though.


Reference:

C99 Standard: 7.20.4.3 The exit function
Para 5

Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE , an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.


exit in the C language takes an integer representing an exit status.

Exit Success

Typically, an exit status of 0 is considered a success, or an intentional exit caused by the program's successful execution.

Exit Failure

An exit status of 1 is considered a failure, and most commonly means that the program had to exit for some reason, and was not able to successfully complete everything in the normal program flow.

Here's a GNU Resource talking about Exit Status.


As @Als has stated, two constants should be used in place of 0 and 1.

EXIT_SUCCESS is defined by the standard to be zero.

EXIT_FAILURE is not restricted by the standard to be one, but many systems do implement it as one.


exit(0) indicates that the program terminated without errors. exit(1) indicates that there were an error.

You can use different values other than 1 to differentiate between different kind of errors.


The difference is the value returned to the environment is 0 in the former case and 1 in the latter case:

$ ./prog_with_exit_0
$ echo $?
0
$

and

$ ./prog_with_exit_1
$ echo $?
1
$

Also note that the macros value EXIT_SUCCESS and EXIT_FAILURE used as an argument to exit function are implementation defined but are usually set to respectively 0 and a non-zero number. (POSIX requires EXIT_SUCCESS to be 0). So usually exit(0) means a success and exit(1) a failure.

An exit function call with an argument in main function is equivalent to the statement return with the same argument.