main return codes

F

Fao, Sean

As far as I can tell, the standard has defined three portable return
codes from function main() (0, EXIT_SUCCESS, EXIT_FAILURE). Personally,
on all platforms I have worked with, EXIT_SUCCESS is, "#define
EXIT_SUCCESS 0" and EXIT_FAILURE is, "#define EXIT_FAILURE 1". I have,
however, been told that a few platforms define the two in reverse
(#define EXIT_SUCCESS 1 and #define EXIT_FAILURE 0). If this is true, I
would expect that it would be common etiquette to use EXIT_SUCCESS or
EXIT_FAILURE for nearly all applications and avoid the use of "return
0;" so as not to accidentally flag a failure on those platforms that do
not comply with the "norm". However, in most of the software projects I
have worked with, "return 0" is the most common return code I've come
across.

What do you all think? I've pretty much started including stdlib.h in
all software projects that I work on and avoid using "return 0;". Just
kind of curious what you opinions are.

Thank you,
 
J

Joona I Palaste

Fao said:
As far as I can tell, the standard has defined three portable return
codes from function main() (0, EXIT_SUCCESS, EXIT_FAILURE). Personally,
on all platforms I have worked with, EXIT_SUCCESS is, "#define
EXIT_SUCCESS 0" and EXIT_FAILURE is, "#define EXIT_FAILURE 1". I have,
however, been told that a few platforms define the two in reverse
(#define EXIT_SUCCESS 1 and #define EXIT_FAILURE 0). If this is true, I
would expect that it would be common etiquette to use EXIT_SUCCESS or
EXIT_FAILURE for nearly all applications and avoid the use of "return
0;" so as not to accidentally flag a failure on those platforms that do
not comply with the "norm". However, in most of the software projects I
have worked with, "return 0" is the most common return code I've come
across.
What do you all think? I've pretty much started including stdlib.h in
all software projects that I work on and avoid using "return 0;". Just
kind of curious what you opinions are.

AFAIK, the C standard defines that 0 means a successful exit. So a
platform is not allowed to #define EXIT_FAILURE 0.
 
F

Flash Gordon

As far as I can tell, the standard has defined three portable return
codes from function main() (0, EXIT_SUCCESS, EXIT_FAILURE). Personally,
on all platforms I have worked with, EXIT_SUCCESS is, "#define
EXIT_SUCCESS 0" and EXIT_FAILURE is, "#define EXIT_FAILURE 1". I have,
however, been told that a few platforms define the two in reverse
(#define EXIT_SUCCESS 1 and #define EXIT_FAILURE 0). If this is true, I
would expect that it would be common etiquette to use EXIT_SUCCESS or
EXIT_FAILURE for nearly all applications and avoid the use of "return
0;" so as not to accidentally flag a failure on those platforms that do
not comply with the "norm". However, in most of the software projects I
have worked with, "return 0" is the most common return code I've come
across.

What do you all think? I've pretty much started including stdlib.h in
all software projects that I work on and avoid using "return 0;". Just
kind of curious what you opinions are.

I very much doubt that any vaguely modern compiler defines EXIT_FAILURE
as 0. The reason being that the ANSI C standard published in 89 (and all
subsequent C standards to date) define a return value of 0 as indicating
success.

What you might be thinking of is VMS where any even value returned to
the OS indicates failure and any odd value indicate success. On such
systems the implementation (probably in the code that calls main) checks
to see if main is returning 0 to the environment and changes it to an
odd number. However, the C program does not have to worry about this,
since it is handled for you.
 
R

Richard Bos

Joona I Palaste said:
AFAIK, the C standard defines that 0 means a successful exit. So a
platform is not allowed to #define EXIT_FAILURE 0.

Yes. IIRC the system in question is MVS (maybe VMS; I've never used
either, and if they insist on using such confusable abbreviations, I
hold them to blame...), EXIT_FAILURE was #defined to be 2 or some other
non-zero even integer, and the compiler did some fiddling behind the
screens to ensure that a return value of 0 was changed to an odd value
after the program exits.
In any case, both 0 and EXIT_SUCCESS must return a succesful exit status
to the environment. Interestingly, there's nothing that demands that
they both return the same succesful; I suppose that on a sytem which
sees negative values as failure and non-negative as success, an
implementation which #defined EXIT_FAILURE as -1 and EXIT_SUCCESS as 1,
and returns these to the environment unchanged, would be conforming.
This would give the C programmer two ways to succeed, but only one to
fail - a situation completely at odds with any programmer's experience,
of course, but it must be bliss to program in such an environment :)

Richard
 
F

Fao, Sean

Joona said:
AFAIK, the C standard defines that 0 means a successful exit. So a
platform is not allowed to #define EXIT_FAILURE 0.

Oh, I forgot to mention that my entire post assumes that the people that
told me some platforms #define EXIT_FAILURE 0, actually knew what they
were talking about. My entire argument is moot if they're just spitting
out a bunch of hot air ;-).
 
K

Keith Thompson

Flash Gordon said:
I very much doubt that any vaguely modern compiler defines
EXIT_FAILURE as 0. The reason being that the ANSI C standard published
in 89 (and all subsequent C standards to date) define a return value
of 0 as indicating success.

I very much doubt that any compiler, modern or not, defines
EXIT_FAILURE as 0. The name EXIT_FAILURE was introduced by the ANSI
standard, which simultaneously decreed that 0 denotes success. (It's
conceivable that some earlier draft might have allowed
EXIT_FAILURE==0, but I doubt it.)
What you might be thinking of is VMS where any even value returned to
the OS indicates failure and any odd value indicate success. On such
systems the implementation (probably in the code that calls main)
checks to see if main is returning 0 to the environment and changes it
to an odd number. However, the C program does not have to worry about
this, since it is handled for you.

On the other hand, a lot of code assumes that exit(1) denotes failure.
This is true on Unix-like systems, but it's not guaranteed by the
standard. (I've seen programs under VMS that made this assumption.)
 
M

Mark McIntyre

AFAIK, the C standard defines that 0 means a successful exit. So a
platform is not allowed to #define EXIT_FAILURE 0.

Its allowed to define it like that, provided the implementation correctly
translates it to mean failure. I suspect that doing so would require some
impossible magic.....

[VMS by the way had a zillion possible failure codes, mostly odd numbers. I seem
to recall that by returning a random odd value you could scare the t*ts off your
users with such gems as
SYS-I-SHUTDOWN the cluster is shutting down at your request
that was always a great one to get the first year students with....]
 
E

Eric Sosman

Mark said:
Its allowed to define it like that, provided the implementation correctly
translates it to mean failure. I suspect that doing so would require some
impossible magic.....

The host environment receives an "implementation-defined
form" of the exit status. An unhelpful implementation could
use the same form for every exit status, making it impossible
for the environment to distinguish between success and failure.
On such an implementation, EXIT_SUCCESS and EXIT_FAILURE could
have the same value.

So the magic isn't impossible, just perverse.
[VMS by the way had a zillion possible failure codes, mostly odd numbers. I seem
to recall that by returning a random odd value you could scare the t*ts off your
users with such gems as
SYS-I-SHUTDOWN the cluster is shutting down at your request
that was always a great one to get the first year students with....]

<off-topic accuracy="uncertain; it's been a while">

s/odd/even/

IIRC, the (Open)VMS status code is a 32-bit quantity with
several fields identifying a "facility," a "message," and a
"severity" in the low-order three bits. Five of the possible
eight severity codes are actually used: Success, Informational,
Warning, Error, and Fatal. Success and Informational (which
is a sort of "Success, and, oh, by the way ...") are odd and
the three sorts of failure are even, so the low-order bit gives
a coarse success/failure indication.

When I first used VMS, a C program's exit status was passed
back verbatim, so exit(0) meant "Warning" and exit(1) meant
"Success." At some point Digital changed the library to translate
zero and one to full-fledged success and failure codes while
leaving other exit() arguments untouched so you could return a
full-fledged status code of your own if desired.

</off-topic>
 
K

Keith Thompson

Mark McIntyre said:
Its allowed to define it like that, provided the implementation correctly
translates it to mean failure. I suspect that doing so would require some
impossible magic.....

exit(EXIT_FAILURE) is required to indicate failure; exit(0) is
required to indicate success. An implementation that defines
EXIT_FAILURE as 0 cannot meet both requirements (unless you consider
failure and success to be synonymous).
[VMS by the way had a zillion possible failure codes, mostly odd
numbers.

As I recall, all odd numbers are failure codes, and all even numbers
are success codes. (And VMS still exists, as OpenVMS, so the past
tense is inappropriate.)
 
K

Keith Thompson

Keith Thompson said:
[VMS by the way had a zillion possible failure codes, mostly odd
numbers.

As I recall, all odd numbers are failure codes, and all even numbers
are success codes. (And VMS still exists, as OpenVMS, so the past
tense is inappropriate.)

Correction: all odd numbers are success codes, and all even numbers
(including 0) are failure codes. Thanks to Eric Sosman for reminding
me.

The VMS C runtime system translates exit(0) to return an odd-numbered
success code. It doesn't translate exit(1), so the common (but
non-standard) use of exit(1) to denote failure doesn't work under VMS.
 
P

Peter Nilsson

Keith said:
exit(EXIT_FAILURE) is required to indicate failure; exit(0) is
required to indicate success. An implementation that defines
EXIT_FAILURE as 0 cannot meet both requirements (unless you
consider failure and success to be synonymous).

ITYM unless the host, if indeed there is a host in any significant
sense, considers failure and success to be synonymous.

This is not as theoretical as it sounds. Under the old MacOS systems,
an application generally terminated via the void ExitToShell(void)
trap. So, _nothing_ was returned to the host.

There were mac implementations like MPW which could and did utilise
the return status. And of course, MacOS X is *nix based, so it too
can utilise the return status.
 
J

James McIninch

<posted & mailed>

Use of

return 0;

.... as a return from main() is simply sloppy, from a C perspective. There
are other languages, such as C++ where it's valid, however, so
inexperienced programmers may be confused.
 
K

Keith Thompson

James McIninch said:
<posted & mailed>

Use of

return 0;

... as a return from main() is simply sloppy, from a C perspective. There
are other languages, such as C++ where it's valid, however, so
inexperienced programmers may be confused.

No, there's nothing sloppy about it. Why do you think there is?

And please don't top-post.
 
M

Martin Ambuhl

James said:
<posted & mailed>

Use of

return 0;

... as a return from main() is simply sloppy, from a C perspective.

Huh? What makes you think so?
There
are other languages, such as C++ where it's valid, however, so
inexperienced programmers may be confused.

Does your confusion come from inexperience? There is nothing sloppy
about using 'return 0;'.
 
C

CBFalconer

*** Top-posting fixed ***
James said:
Fao, Sean wrote:
.... snip ...

Use of
return 0;

... as a return from main() is simply sloppy, from a C perspective.
There are other languages, such as C++ where it's valid, however,
so inexperienced programmers may be confused.

Please do not toppost. Use of "return 0" in main is not in the
least sloppy, it is documented in the C standard. The primary
advantage is that you don't have to #include <stdlib.h> to use it.
However "return 1" is anathema.
 
P

pete

James said:
<posted & mailed>

Use of

return 0;

... as a return from main() is simply sloppy, from a C perspective.

From a C perspective, it's just as neat as anything could be.
There
are other languages, such as C++ where it's valid, however, so
inexperienced programmers may be confused.

You are now an inexperienced programmer,
despite your status yesterday.
 
F

Fao, Sean

Well after reading all your responses, I see now that it may have been
my own misinterpretation. Learn something new every day ;-).

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top