Should I return EXIT_SUCCESS or 0 from main()?

前端 未结 8 1100
梦毁少年i
梦毁少年i 2020-11-27 09:54

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

         


        
相关标签:
8条回答
  • 2020-11-27 10:32

    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...

    0 讨论(0)
  • 2020-11-27 10:34

    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).

    0 讨论(0)
提交回复
热议问题