168

It's a simple question, but I keep seeing conflicting answers: should the main routine of a C++ program return 0 or EXIT_SUCCESS?

#include <cstdlib>
int main(){return EXIT_SUCCESS;}

or

int main(){return 0;}

Are they the exact same thing? Should EXIT_SUCCESS only be used with exit()?

I thought EXIT_SUCCESS would be a better option because other software may want to deem zero as failure, but I also heard that if you return 0, the compiler is capable of changing it to a different value anyway.

Jamal
  • 763
  • 7
  • 22
  • 32
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • Does this answer your question? http://stackoverflow.com/questions/1188335/why-default-return-value-of-main-is-0-and-not-exit-success – Blastfurnace Jan 15 '12 at 06:14
  • 2
    [This answer about C90](http://stackoverflow.com/a/207992/242520) paraphrases the standard -- `0` and `EXIT_SUCCESS` are both interpreted as success. – ta.speot.is Jan 15 '12 at 06:16

9 Answers9

201

EXIT_FAILURE, either in a return statement in main or as an argument to exit(), is the only portable way to indicate failure in a C or C++ program. exit(1) can actually signal successful termination on VMS, for example.

If you're going to be using EXIT_FAILURE when your program fails, then you might as well use EXIT_SUCCESS when it succeeds, just for the sake of symmetry.

On the other hand, if the program never signals failure, you can use either 0 or EXIT_SUCCESS. Both are guaranteed by the standard to signal successful completion. (It's barely possible that EXIT_SUCCESS could have a value other than 0, but it's equal to 0 on every implementation I've ever heard of.)

Using 0 has the minor advantage that you don't need #include <stdlib.h> in C, or #include <cstdlib> in C++ (if you're using a return statement rather than calling exit()) -- but for a program of any significant size you're going to be including stdlib directly or indirectly anyway.

For that matter, in C starting with the 1999 standard, and in all versions of C++, reaching the end of main() does an implicit return 0; anyway, so you might not need to use either 0 or EXIT_SUCCESS explicitly. (But at least in C, I consider an explicit return 0; to be better style.)

(Somebody asked about OpenVMS. I haven't used it in a long time, but as I recall odd status values generally denote success while even values denote failure. The C implementation maps 0 to 1, so that return 0; indicates successful termination. Other values are passed unchanged, so return 1; also indicates successful termination. EXIT_FAILURE would have a non-zero even value.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • @KeithThompson could you clarify your answer with respect to VMS (OpenVMS?). It is not clear the relation EXIT_SUCCESS / EXIT_FAILURE with 0 and 1. I would explain odd/even values instead. – malat Dec 06 '16 at 07:20
  • @malat: Do you actually use VMS? – Keith Thompson Dec 06 '16 at 07:21
  • no, never used it. I was looking a canonical answer, since wikipedia used a [different wording](https://en.wikipedia.org/wiki/Exit_status#OpenVMS). – malat Dec 06 '16 at 07:25
  • 1
    How is it even possible to implement POSIX utilities for OpenVMS when `exit(0)` is mapped to exit status 1? (if I understand your last paragraph correctly) –  Dec 06 '16 at 07:47
  • 3
    @Rhymoid: This is specified by C, not POSIX. – Keith Thompson Dec 06 '16 at 08:10
  • If I understand Mr Thompsons answer, return 0 and return EXIT_SUCCESS are the same as guaranteed by the standard. However, coding as EXIT_SUCCESS is syntactically better practice because it is the worded opposite of EXIT_FAILURE. Having said this, we cannot say that EXIT_FAILURE is the same as returning a non zer0 value, for example, return 1. – Dean P Feb 24 '19 at 11:48
  • 1
    @DeanP: `0` and `EXIT_SUCCESS` are not guaranteed to have the same value (I mentioned that in my answer), but they both denote successful termination. `EXIT_SUCCESS` is stylistically better *if* you're also using `EXIT_FAILURE`, but `exit(0)` is ok. – Keith Thompson Feb 25 '19 at 08:47
  • 2
    This is personal (for same motive: explicit), but I like to "return(0)" (or != 0) because shell script "$?" compatibility (the majority unix programs return "$?"=0 for success). – André A. G. Scotá Mar 15 '19 at 02:59
  • 2
    @AndréA.G.Scotá: POSIX guarantees `EXIT_SUCCESS==0`. (And you don't need parentheses on a `return` statement. Personally, I think they make it look too much like a function call.) – Keith Thompson Mar 15 '19 at 04:03
33

It does not matter. Both are the same.

C++ Standard Quotes:

If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 19
    It does not matter to the compiler, but it may matter as a matter of style. – celtschk Jan 15 '12 at 08:13
  • 2
    @celtschk: Matter of style is perception based a.k.a Non Standardized so that does not count as an difference.You can only compare Apples with Apples not Apples with Pears. – Alok Save Jan 15 '12 at 08:26
  • 3
    There is no guarantee that `EXIT_SUCCESS == 0`. On the other hand, there's no good reason for it not to be. – Keith Thompson Oct 08 '14 at 01:24
  • @KeithThompson: why There is no guarantee that EXIT_SUCCESS == 0.? Please explain it more clearly. – Destructor Jun 22 '15 at 12:18
  • 2
    @PravasiMeet: A system might have more than one value that indicates success. – Keith Thompson Jun 22 '15 at 14:20
  • 2
    Function over form, but clarity over function. I can tell you from professional experience that style matters a great deal in programming. –  Dec 06 '16 at 07:43
18

0 is, by definition, a magic number. EXIT_SUCCESS is almost universally equal to 0, happily enough. So why not just return/exit 0?

exit(EXIT_SUCCESS); is abundantly clear in meaning.

exit(0); on the other hand, is counterintuitive in some ways. Someone not familiar with shell behavior might assume that 0 == false == bad, just like every other usage of 0 in C. But no - in this one special case, 0 == success == good. For most experienced devs, not going to be a problem. But why trip up the new guy for absolutely no reason?

tl;dr - if there's a defined constant for your magic number, there's almost never a reason not to used the constant in the first place. It's more searchable, often clearer, etc. and it doesn't cost you anything.

James
  • 8,512
  • 1
  • 26
  • 28
  • 2
    I think your comments about people expecting 0 to be automatically bad is off the mark. Very many APIs use 0 for success, and non-0 for failure, even in the stdlib. E.g. stdlib (`fclose()`, `setvbuf()`, ...), POSIX (`listen()`, `pthread_create()`, `pipe()`, ...), and many, ***many*** other libraries (e.g. OpenGL [`glGetError()`], zlib [`deflate()`/`inflate()`/...], SDL [`SDL_CreateWindowAndRenderer()`/...], and more). – Tim Čas Oct 02 '17 at 19:59
  • 3
    Sure, there are other instances where 0 is used for 'success,' but he's right about it being confusing. – Paul Wintz Mar 26 '18 at 17:56
12

This is a never ending story that reflect the limits (an myth) of "interoperability and portability over all".

What the program should return to indicate "success" should be defined by who is receiving the value (the Operating system, or the process that invoked the program) not by a language specification.

But programmers likes to write code in "portable way" and hence they invent their own model for the concept of "operating system" defining symbolic values to return.

Now, in a many-to-many scenario (where many languages serve to write programs to many system) the correspondence between the language convention for "success" and the operating system one (that no one can grant to be always the same) should be handled by the specific implementation of a library for a specific target platform.

But - unfortunatly - these concept where not that clear at the time the C language was deployed (mainly to write the UNIX kernel), and Gigagrams of books where written by saying "return 0 means success", since that was true on the OS at that time having a C compiler.

From then on, no clear standardization was ever made on how such a correspondence should be handled. C and C++ has their own definition of "return values" but no-one grant a proper OS translation (or better: no compiler documentation say anything about it). 0 means success if true for UNIX - LINUX and -for independent reasons- for Windows as well, and this cover 90% of the existing "consumer computers", that - in the most of the cases - disregard the return value (so we can discuss for decades, bu no-one will ever notice!)

Inside this scenario, before taking a decision, ask these questions: - Am I interested to communicate something to my caller about my existing? (If I just always return 0 ... there is no clue behind the all thing) - Is my caller having conventions about this communication ? (Note that a single value is not a convention: that doesn't allow any information representation)

If both of this answer are no, probably the good solution is don't write the main return statement at all. (And let the compiler to decide, in respect to the target is working to).

If no convention are in place 0=success meet the most of the situations (and using symbols may be problematic, if they introduce a convention).

If conventions are in place, ensure to use symbolic constants that are coherent with them (and ensure convention coherence, not value coherence, between platforms).

danglingpointer
  • 4,708
  • 3
  • 24
  • 42
Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
4

Once you start writing code that can return a myriad of exit statuses, you start #define'ing all of them. In this case EXIT_SUCCESS makes sense in context of not being a "magic number". This makes your code more readable because every other exit code will be EXIT_SOMETHING. If you simply write a program that will return when it's done, return 0 is valid, and probably even cleaner because it suggests that there's no sophisticated return code structure.

Phonon
  • 12,549
  • 13
  • 64
  • 114
0

What you return from a program is just a convention.

No, I can't think of any circumstances where "EXIT_SUCCESS" wouldn't be "0".

Personally, I'd recommend "0".

IMHO...

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 2
    I have been programming C++ for 10 years and still can't remember if 0 means success or failure in terms of return values. – lahjaton_j Nov 30 '16 at 13:11
  • 2
    @lahjaton_j I understand. It think it's because it's counter intuitive: `0` is false, and many functions return `0` on failure/doing nothing. – meaning-matters Jan 20 '19 at 18:08
  • Very true. Hence the old joke (since 0 is false) about the purpose of `main()` being to run your program and tell you whether it _failed_ or not. – Ray Toal Sep 03 '23 at 03:26
-2

tl;dr: You could just not return anything.

It is ok (Bjarne Stroustrup says so...) to not-explicitly-return anything from the main() function. The compiler will take care things if your OS expects a return value.

This is not to detract from the discussion in the answers by @KeithThompson or @EmilioGaravaglia regarding the case of different possible return value, and being aware of how your program's return value gets interpreted etc.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
-4

If you use EXIT_SUCCESS, your code will be more portable.

http://www.dreamincode.net/forums/topic/57495-return-0-vs-return-exit-success/

Mohammad Moghimi
  • 4,636
  • 14
  • 50
  • 76
-5

Some compilers might create issues with this - on a Mac C++ compiler, EXIT_SUCCESS worked fine for me but on a Linux C++ complier I had to add cstdlib for it to know what EXIT_SUCCESS is. Other than that, they are one and the same.

Ambidextrous
  • 810
  • 6
  • 14
  • I meant on a Mac EXIT_SUCCESS worked without including cstdlib. – Ambidextrous Jan 15 '12 at 06:26
  • 7
    If `EXIT_SUCCESS` worked without including either `` or ``, some other header must have defined it, directly or indirectly. In C++, it's common for standard headers to `#include` other standard headers. If this compiles without error: `int main() { return EXIT_SUCCESS; }` then your compiler is probably buggy. – Keith Thompson Oct 08 '14 at 01:24