1

Possible Duplicate:
(C/C++) return EXIT_SUCCESS or 0 from main?

For a long time I was using return 0;, from the time I started to use allegro. Than I came across C++ book and found about EXIT_SUCCESS, than I looked into stdlib.h or cstdlib where it's defined and there is only two lines:

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

can somebody explain this to me? I am using mingw version of the last devc++ 4.9.9.2.

Community
  • 1
  • 1
SomKtoSom
  • 49
  • 1
  • 7
  • What's to explain? You should typically return 0 from `main` to indicate success and any other value for failure. – chris Nov 02 '12 at 21:43
  • you can use either, however there is no standard way to exit with failure unless you use EXIT_FAILURE. see http://stackoverflow.com/questions/8867871/c-c-return-exit-success-or-0-from-main – Trevor Hickey Nov 02 '12 at 21:50
  • @chris `0` is required to mean success, but there is no other requirement concerning values other than `EXIT_FAILURE` and `EXIT_SUCCESS`. Even in common systems: under Unix, `256` will be treated as success (because the OS only keeps the low order 8 bits), and in other systems, the sky's the limit: negative or odd values are failure, or... Also of course: Unix and Windows simply pass this value up to the invoking program. Which interprets it as it wishes. C++ can express an intention here, but the program which invokes your program isn't subject to the C++ standard. – James Kanze Nov 02 '12 at 22:29
  • @JamesKanze, Yes, I should have been more clear. I was talking in context of a program calling yours. Zero is, by convention, success, and I can't talk with too much experience about other OSes, but Windows is conventionally any other value for failure. – chris Nov 02 '12 at 22:51
  • @chris Don't worry about it. I'm being a bit pedantic. But the fact remains that the C++ standard can only specify C++, and that everything in it which involves interaction with the external environment can really only be taken as intent in the standard, because the C++ standard can't specify the external environment. It cannot, for example, prevent me from writing a script which tests whether `$? = 3` to determine that the program has succeeded. (But that's obviously _not_ the convention in the Unix world.) – James Kanze Nov 02 '12 at 23:13

2 Answers2

7

The C99 language standard defines the two macros EXIT_SUCCESS and EXIT_FAILURE to expand to "integer constant expressions that can be used as the argument to the exit function to return unsuccessful or successful termination status, respectively, to the host environment" (§7.20/3).

In the description of the exit function (§7.20.4.3/5), it also says:

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.

So, if you exit with 0 or EXIT_SUCCESS, that always means "successful termination" to the host environment. EXIT_FAILURE is a value that will always mean "unsuccessful termination", but any non-zero value other than those is not guaranteed to be portable.

Note that returning from main is equivalent to calling the exit function with a status of the return value (§5.1.2.2.3).

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • The remark about return from main is true in C, but it is *not* true in C++. `exit` does not call destructors of local variables. `return` from main, like return from any other function, will call destructors as the objects go out of scope. – Andrew Lazarus Jan 14 '15 at 19:47
1

EXIT_SUCCESS and EXIT_FAILURE are Standard C macros defined in stdlib.h. C does not specify the value of these macros.

POSIX requires EXIT_SUCCESS to be 0. (But does not specify anything for EXIT_FAILURE value).

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    C does not specify the value of `EXIT_SUCCESS` but it *does* specify that returning `0` from `main` has the same *effect* as returning `EXIT_SUCCESS`. There *were* platforms (older VMS, if memory serves) that did not define `EXIT_SUCCESS` as zero *and* did not treat returning zero from main as successful termination, but that was a failure to conform to the standard. – zwol Nov 02 '12 at 21:55
  • @Zack it means `EXIT_SUCCESS` value can be 42 even if the value `0` means a successfull termination. – ouah Nov 02 '12 at 22:01
  • 2
    Sure it can; all I'm saying is `return 0;` from `main` is a perfectly conformant way to terminate a program successfully. – zwol Nov 02 '12 at 23:01