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

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

+55
c
Mar 30 '12 at 14:20
source share
10 answers

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

exit(0) indicates successful completion of the program and is completely carried forward, while exit(1) (usually) indicates failure. However, use is not tolerated.

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

0 and EXIT_SUCCESS are values ​​specified by the standard to indicate success, but only EXIT_FAILURE is the standard value to return failure. 1 used for the same thing in many implementations.




Link:

C99 Standard: 7.20.4.3 exit function
Paragraph 5

Finally, control returns to the host environment. If the status value is zero or EXIT_SUCCESS , the implementation-defined form for successful completion is returned. If the status value is EXIT_FAILURE , the form determined by the implementation of the failure status is returned. Otherwise, the implementation status is returned, de defined.

+77
Mar 30 2018-12-12T00:
source share

exit(0) indicates that the program terminated without errors. exit(1) indicates that an error has occurred.

You can use different values ​​than 1 to distinguish between different types of errors.

+9
Mar 30 2018-12-12T00:
source share

exit in C takes an integer representing the exit status.

Exit success

As a rule, exit status 0 is considered successful or intentional exit caused by the successful execution of the program.

Failure

Output state 1 is considered unsuccessful, and most often this means that the program had to exit for some reason, and it could not successfully complete everything in the normal program flow.

Here's the GNU Resource talking about exit status.




As pointed out by @Als, two constants should be used instead of 0 and 1.

EXIT_SUCCESS defined by the standard as zero.

EXIT_FAILURE not limited to the standard as a single, but many systems implement it as one.

+9
Mar 30 2018-12-12T00:
source share

The difference is the value returned on Wednesday, 0 in the first case and 1 in the latter case:

 $ ./prog_with_exit_0 $ echo $? 0 $ 

and

 $ ./prog_with_exit_1 $ echo $? 1 $ 

Also note that the macro values EXIT_SUCCESS and EXIT_FAILURE , used as an argument to the exit function, are determined by the implementation, but usually 0 and a nonzero number are set respectively. (POSIX requires EXIT_SUCCESS to be 0). Therefore, usually exit(0) means success and exit(1) fails.

A function calling an exit function with an argument in the main function is equivalent to a return expression with the same argument.

+3
Mar 30 2018-12-12T00:
source share

exit(0) behave like return 0 in main() , exit(1) behave like return 1 . The standard is that main function return 0 if the program completed successfully, and a nonzero value means that the program was interrupted with some error.

+2
Mar 30 2018-12-12T00:
source share

exit is a system call used to terminate the current process from which it is called. The exit parameter is used to inform the parent process of the status of the child process. Thus, exit (0) can be used (and often used) to indicate successful execution of the process and exit (1) to mark an error. link link

+2
Aug 12 '15 at 9:52
source share

When the executable finishes (completes), it returns the value to the shell that started it. exit(0) usually indicates that everything is fine, and exit(1) indicates that something went wrong.

+1
Mar 30 2018-12-12T00:
source share

exit () should always be called with an integer value, and nonzero values ​​are used as error codes.

See also: Using exit ()

+1
Mar 30 2018-12-12T00:
source share

exit(0) means that the program (process) terminates normally successfully.

exit(1) means that the program (process) usually ends unsuccessfully.

If you want to watch this thing, you need to know signal processing and process control on Unix ...

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

+1
May 12 '12 at 12:02
source share

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

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

On failure, it usually returns some positive value to exit the process, which you can find in the shell with $ ?.

A value greater than 128, which is caused by signal termination. Therefore, if any shell command is completed with a signal, the return status should be (128 + signal number).

For example:

If any shell command terminates in SIGINT, then $? will give 130 (128 + 2) (Here 2 is the signal number for SIGINT, check with kill -l)

0
Mar 25 '17 at 20:22
source share



All Articles