68

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

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220

5 Answers5

79

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Alok Save
  • 202,538
  • 53
  • 430
  • 533
16

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
6

Use EXIT_FAILURE. It is a constant that is used throughout the OS. Its value could be something else than 1 and also it is more descriptive in the code.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Is it a constant throughout *multiple* OS's? – temporary_user_name Dec 02 '12 at 07:42
  • 7
    @Aerovistae: `EXIT_FAILURE` is specified by the C standard, and has been since 1989. It will be available on any reasonably modern C compiler. Its value is typically `1`, but it will always be the correct value that denotes failed program execution in the current OS. – Keith Thompson Nov 18 '14 at 19:05
3

There are conventions for what sorts of status values certain programs should return. The most common convention is simply 0 for success and 1 for failure. Programs that perform comparison use a different convention: they use status 1 to indicate a mismatch, and status 2 to indicate an inability to compare. Your program should follow an existing convention if an existing convention makes sense for it.

Some non-POSIX systems use different conventions for exit status values. For greater portability, you can use the macros EXIT_SUCCESS and EXIT_FAILURE for the conventional status value for success and failure, respectively. They are declared in the file stdlib.h.

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
1

Not only VMS has its own rules also AmigaDOS defines a return code below 5 as okay.

Polluks
  • 525
  • 2
  • 8
  • 19