views:

2334

answers:

10

Hello,

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 ?

Thanks by advance.

+4  A: 

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
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
"EXIT_STATUS couldhave a value different from 0, and in fact it did onVAX/VMS at one point in time": taken from http://bytes.com/groups/c/215177-exit_success-guaranteed-always-zero
xtofl
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 ...".
Serious
+5  A: 

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
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 ...
Serious
A: 

These standards came from C which C++ is largely based.

Charles Ma
OK. So what's the answer to the question?
Rob Kennedy
+1  A: 

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
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
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
A: 

As a matter of convention or standard (it really doesn't matter), zero has been the long-standing "success" exit code. Have you ever looked at errno.h?

Regardless of that, defining a standard success exit code doesn't, in any way, force semantics upon a programmer. I'm not sure you have a clear understanding of what semantics in computer science actually is: semantics reflects the meaning. Making a standard for exit codes does not change the meaning of a program.

As for your error code question, it is a very common practice to use different exit codes to communicate more specific information on why/how the program exited. Again, look at errno.h - it gives programmers a way to say "hey, this function failed because of this particular reason." Again, this comes from the C foundation of C++.

Have you ever programmed in C?

Matt Ball
+18  A: 

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.

Michael Burr
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
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
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" ?
Serious
@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
@Jalf - I left out that detail as I didn't think that added much to the answer. Keep it simple...
Michael Burr
@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
@Michael: Yeah, I'm not saying you should have necessarily added that information, I just made it available in a comment :)
jalf
@Michael : Thanks a lot for these answers.
Serious
+2  A: 

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
+10  A: 

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
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
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
@Bastien : Thanks, your comments complete the explanation of Michael.
Serious
+1  A: 

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
+1  A: 

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

-- glen

glen
Interesting historical information.
Serious