Why default return value of main is 0 and not EXIT

2019-01-18 02:48发布

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?

9条回答
姐就是有狂的资本
2楼-- · 2019-01-18 02:59

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?

查看更多
唯我独甜
3楼-- · 2019-01-18 03:00

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.

查看更多
forever°为你锁心
4楼-- · 2019-01-18 03:03

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

查看更多
5楼-- · 2019-01-18 03:05

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.

查看更多
Bombasti
6楼-- · 2019-01-18 03:05

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.

查看更多
疯言疯语
7楼-- · 2019-01-18 03:05

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

查看更多
登录 后发表回答