Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXIT_FAILURE vs exit(1)?

Tags:

c

exit

What's the difference? Which is preferred, or when should I use each one respectively?

like image 522
temporary_user_name Avatar asked Dec 02 '12 07:12

temporary_user_name


People also ask

What is exit Exit_failure?

#define EXIT_FAILURE /*implementation defined*/ The EXIT_SUCCESS and EXIT_FAILURE macros expand into integral expressions that can be used as arguments to the exit function (and, therefore, as the values to return from the main function), and indicate program execution status.

What is the difference between Exit 0 and Exit 1?

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.

What is value of Exit_failure?

The value of EXIT_SUCCESS is defined in stdlib. h as 0; the value of EXIT_FAILURE is 8.


2 Answers

exit(1) (usually) indicates unsuccessful termination. However, its usage is non-portable. For example, on OpenVMS, exit(1) actually indicates success.

Only EXIT_FAILURE is the standard value for returning unsuccessful termination, but 1 is used for the same in many implementations.


So to summarize:
If you want to write perfectly portable code use,

EXIT_FAILURE for failure case. While,
You can use either exit(0) or EXIT_SUCCESS for success case.

Note that, EXIT_SUCCESS or 0 are both same.


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.

like image 121
Alok Save Avatar answered Sep 22 '22 16:09

Alok Save


For truly portable code, EXIT_FAILURE is preferred. The C standard only defines meaning for three values: EXIT_FAILURE, 0, and EXIT_SUCCESS (with 0 and EXIT_SUCCESS essentially synonymous).

From a practical viewpoint, most typical systems accept other values as well. If memory serves, Linux will let you return any 8-bit value, and Windows 16-bit values. Unless you honestly might care about porting to an IBM mainframe, VMS, etc., chances are you don't care about most of the systems that won't support at least 8-bit return values.

like image 21
Jerry Coffin Avatar answered Sep 25 '22 16:09

Jerry Coffin