111

Can anyone tell me? What is the difference between exit(0) and exit(1) in C language?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

11 Answers11

131

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    Can you explain the non-portability? In particular, nonzero exit codes indicate different failure modes. It’s common for an application to use this to provide further information. A single constant (`EXIT_FAILURE`) is thus insufficient. – Konrad Rudolph Mar 30 '12 at 14:34
  • @KonradRudolph: I added the relevant citation. Hope that helps. – Alok Save Mar 30 '12 at 14:38
  • It's portable on POSIX and Windows. – Cat Plus Plus Mar 30 '12 at 14:42
  • @CatPlusPlus: Not by virtue of standard specification but by virtue of implementation specifics. – Alok Save Mar 30 '12 at 14:43
  • @CatPlusPlus C has been ported to a lot more than just those systems – Peter M Mar 30 '12 at 14:43
  • 1
    @Als: POSIX is a standard. C standard might say "implementation-defined" but that's not equivalent to saying "not portable", when implementations in fact *do* agree on the convention. – Cat Plus Plus Mar 30 '12 at 14:51
  • 1
    @PeterM: Find me a platform that doesn't use this convention. It's likely to be some specialised/embedded/freestanding/whatever environment, where portability of `exit` is the least of your concerns (hell, there might not even *be* a C library available there. NOTHING IS PORTABLE!!!111). – Cat Plus Plus Mar 30 '12 at 14:52
  • 4
    @CatPlusPlus: On OpenVMS, calling `exit` with any odd value denotes success. `exit(0)` is treated as a special case for the sake of C conformance. Yes, POSIX is a standard, but not all systems conform to it. If you want to write code that assumes POSIX, you're free to do so, but it limits your code's portability. – Keith Thompson Aug 20 '13 at 00:46
13

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.

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
9

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.

sch
  • 27,436
  • 3
  • 68
  • 83
4

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.

ouah
  • 142,963
  • 15
  • 272
  • 331
2

exit is a system call used to finish a running process from which it is called. The parameter to exit is used to inform the parent process about the status of child process. So, exit(0) can be used (and often used) to indicate successful execution of a process and exit(1) to flag an error. reference link

mahtab
  • 79
  • 1
  • 6
2

exit(0) is equivalent to exit(EXIT_SUCCESS).

exit(1) is equivalent to exit(EXIT_FAILURE).

On failure normally any positive value get returned to exit the process, that you can find on shell by using $?.

Value more than 128 that is caused the termination by signal. So if any shell command terminated by signal the return status must be (128+signal number).

For example:

If any shell command is terminated by SIGINT then $? will give 130 ( 128+2) (Here 2 is signal number for SIGINT, check by using kill -l )

Sandeep_black
  • 1,352
  • 17
  • 18
2

exit function. In the C Programming Language, the exit function calls all functions registered with at exit and terminates the program.

exit(1) means program(process) terminate unsuccessfully. File buffers are flushed, streams are closed, and temporary files are deleted

exit(0) means Program(Process) terminate successfully.

1

exit(0) means Program(Process) terminate normally successfully..

exit(1) means program(process) terminate normally unsuccessfully..

If you want to observe this thing you must know signal handling and process management in Unix ...

know about sigaction, watipid()..for()...such....API...........

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

exit(0) behave like return 0 in main() function, exit(1) behave like return 1. The standard is, that main function return 0, if program ended successfully while non-zero value means that program was terminated with some kind of error.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Tomáš Šíma
  • 834
  • 7
  • 26
1

When the executable ends (exits) it returns a value to the shell that ran it. exit(0) usually indicates that all is well, whilst exit(1) indicates that something has gone amiss.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

exit() should always be called with an integer value and non-zero values are used as error codes.

See also: Use of exit() function

Community
  • 1
  • 1
Squig
  • 862
  • 6
  • 15