24

The ISO 1998 c++ standard specifies that not explicitly using a return statement in the main is equivalent to use return 0. But what if an implementation has a different standard "no error" code, for example -1?

Why not use the standard macro EXIT_SUCCESS that would be replaced either by 0 or -1 or any other value depending on the implementation?

C++ seems to force the semantic of the program, which is not the role of a language which should only describe how the program behaves. Moreover the situation is different for the "error" return value: only EXIT_FAILURE is a standard "error" termination flag, with no explicit value, like "1" for example.

What are the reasons of these choices?

durron597
  • 31,968
  • 17
  • 99
  • 158
Pragmateek
  • 13,174
  • 9
  • 74
  • 108

9 Answers9

36

Returning zero from main() does essentially the same as what you're asking. Returning zero from main() does not have to return zero to the host environment.

From the C90/C99/C++98 standard document:

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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 4
    just to clarify, that is specified for the exit() function, and 3.6.1:5 states tat the effect of returning from main is to leave main and then call exit(). So yeah, you're right. :) – jalf Jul 27 '09 at 14:48
  • 1
    In other words, zero and `EXIT_SUCCESS` are equivalent return values, so it doesn't matter which one the standard requires be returned by default. – Rob Kennedy Jul 27 '09 at 14:54
  • Very interesting, it means that c++ standard does not force the host "success code". So returning "0" from a c++ program does not guarantee that the host system will receive "0" and means that the behavior of the c++ return statement could be different from what the programmer specified : "return 0" could be on an implementation "replaced" by "return 1" ? – Pragmateek Jul 27 '09 at 15:09
  • 1
    @Serious - remember that a particular environment might not even use numeric return codes in the same way that Unix does. I'd be surprised if there are any that aren't more or less just curiosities today, but for example, I honestly don't know what occurs in a mainframe environment that uses JCL to control program execution. – Michael Burr Jul 27 '09 at 16:00
  • @Jalf - I left out that detail as I didn't think that added much to the answer. Keep it simple... – Michael Burr Jul 27 '09 at 16:01
  • 4
    @Serious: but to answer your question more directly - if a program's execution environment expects 1 to mean success and 0 to mean failure, then a "return 0" from main() should probably set the program's exit code to 1. And a "return EXIT_FAILURE" should probably set the program's exit code to 0. But I expect those environments to be pretty rare, as the C and Unix intepretation of 0 as success has been quite influential. – Michael Burr Jul 27 '09 at 16:08
  • @Michael: Yeah, I'm not saying you should have necessarily added that information, I just made it available in a comment :) – jalf Jul 27 '09 at 16:11
18

Actually, return 0 won't necessarily return 0! I'm quoting the C standard here, because it's what I know best.

About return in main():

5.1.2.2.3 Program termination

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;

About exit():

7.20.4.3 The exit function
Synopsis

#include <stdlib.h>
void exit(int status);

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

Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
  • Always nice to see someone explaining the standard in an understandable way. Still, being able to choose between the literal zero and EXIT_SUCCESS is equivalent to using two possibly conflicting definitions. Am I right to think so? – xtofl Jul 27 '09 at 19:46
  • 5
    Where do you see such a conflict come from? It's the responsibility of the compiler to make it work. Easiest way is to #define EXIT_SUCCESS 0 but that is just one option. – MSalters Jul 28 '09 at 07:53
5

The standard is simply making a determination on what must be the value when not explicitly set. It is up to the developers to either explicitly set the return value or assume an appropriate semantic for the default. I don't think the language is trying to force any semantics on to the developers.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
  • The default value should not be determined by the c++ layer but by the system. For example if a program is first written in c++, "0" will be the standard success code; then if it is rewritten in an other language for which "1" is the standard default success code it would break the behavior of the other programs/user that uses it. Whereas if the two languages have a macro EXIT_SUCCESS that will be managed by the host system all will work fine. The macro would be replaced by the compiler by its value on the system : "0" for posix ones, "1" for other ones ... – Pragmateek Jul 27 '09 at 15:01
4

0 is the standard (success) exit code on all POSIX systems, and all systems I know! I think it has been this way sinc time begain (or at least since Unix did...) So it's for this reason I would say.

What system do you know that is different?

jkp
  • 78,960
  • 28
  • 103
  • 104
  • 3
    IMHO, that's not the issue: if there were any *system* preferring a different value, and the *language* defined what a successful main return value were, this would make the language clash on that system. Next to that, you can't prove a theorem by giving examples. – xtofl Jul 27 '09 at 14:21
  • 2
    "EXIT_STATUS could have a value different from 0, and in fact it did on VAX/VMS at one point in time": taken from http://bytes.com/groups/c/215177-exit_success-guaranteed-always-zero – xtofl Jul 27 '09 at 14:24
  • It's true that "0" as a success flag is a "de facto" standard. But making it the standard for c++ forces the host systems to use it in order to be a "iso compliant" implementation. Whereas the use of a standard constant like EXIT_SUCCESS guarantees that the semantic, "all is ok", is not coupled with the implementation details, "0 means success, 1 failure ...". – Pragmateek Jul 27 '09 at 14:54
3

0 as the return code for success, and postive integers as errors is the standard in C and Unix. This scheme was chosen because normally one doesn't care why a program succeeded, just that it did. On the other hand, there are lots of ways for a program to fail with an error, and one is often interested in this information. Hence it makes sense to use a scalar value for success, and a range of values for error. Using positive integers is a memory-saving C convention, as they allow the error code to be defined as an unsigned int.

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
3

OS/360 and successors use a numeric exit code, 0 usually being success, 4 for warnings (such as a compiler that generated warning message), 8 for error, and 12 for especially bad errors (such as being unable to open SYSPRINT, the standard output unit).

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
glen
  • 31
  • 1
1

Computer language standards say what a program written in the language has to do, and what will happen. In this case, the C and C++ standards say that returning 0 signals success, among other things.

Language implementations allow programs to run on particular implementations. It's the job of the implementor to figure out how to make I/O work according to the standard, or to give the OS the correct result code.

What a program does and what the OS sees do not have to be the same thing. All that is necessary is that the program works as the standard says on the given OS.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
1

By browsing cstdlib I ended up with two lines:

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

So EXIT_SUCCESS equals 0, and EXIT_FAILURE equals 1, which means it doesn't matter, thought.

Checked on Linux (OpenSuse) and ended up with the same thing.

набиячлэвэли
  • 4,099
  • 4
  • 29
  • 40
  • 3
    To be pedantic: these defines are only for your platform, another platform could use a different convention; though I think they're now universal. :) – Pragmateek Oct 06 '13 at 14:47
  • @Pragmateek Of course, it is. This code is from Windows, but I think it's the same for Linux. However, I'll read from Linux and Android and we'll see what will I end with. – набиячлэвэли Oct 21 '13 at 20:27
0

If you were also thinking about why the error code for success is 0 instead of any other value, I would add that it might have been historically for performance reasons, as comparing with 0 have been sightly faster (I think in modern architectures might be the same as with any number) and usually you don't check for a particular error code, just if it was success or any error (so it makes sense to use the fastest comparison for that).

fortran
  • 74,053
  • 25
  • 135
  • 175
  • 1
    I don't think this is the reason for using 0 as a success indicator - I believe the reason is that there is usually more than one way to fail, so there's a need for more than one failure code. – Michael Burr Jul 27 '09 at 14:32
  • That's true, but it doesn't contradict what I've said. If ERROR_SUCCESS were 1 (or whichever) you still have all the other numbers to indicate different kinds of failures. – fortran Jul 27 '09 at 14:37