By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,684 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,684 IT Pros & Developers. It's quick & easy.

main return codes

P: n/a
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,

--
Sean
Nov 14 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Fao, Sean <en**********@yahoo.comi-want-no-spam> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"It sure is cool having money and chicks."
- Beavis and Butt-head
Nov 14 '05 #2

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Fao, Sean <en**********@yahoo.comi-want-no-spam> scribbled the following:
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.


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
Nov 14 '05 #3

P: n/a
Joona I Palaste wrote:
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 ;-).

--
Sean
Nov 14 '05 #4

P: n/a
Fao, Sean wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #5

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
[...] (maybe VMS; I've never used either, and if they insist on using such
confusable abbreviations, I hold them to blame...)


VMS = Virtual Memory System, the best damn operating system ever
developed.
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
ra*********@sonyericsson.com, 919-472-1124
Nov 14 '05 #6

P: n/a
Flash Gordon <sp**@flash-gordon.me.uk> writes:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

P: n/a
On 14 Mar 2005 16:27:01 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
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....]


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #8

P: n/a


Mark McIntyre wrote:
On 14 Mar 2005 16:27:01 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:

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


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>
Nov 14 '05 #9

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On 14 Mar 2005 16:27:01 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
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.....


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

P: n/a
Keith Thompson <ks***@mib.org> writes:
Mark McIntyre <ma**********@spamcop.net> writes:

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11

P: n/a
Keith Thompson wrote:

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.

--
Peter

Nov 14 '05 #12

P: n/a
<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.

Fao, Sean wrote:
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,


--
Remove '.nospam' from e-mail address to reply by e-mail
Nov 14 '05 #13

P: n/a
James McIninch <ja*******************@comcast.net> writes:
<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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14

P: n/a
James McIninch wrote:
<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;'.
Nov 14 '05 #15

P: n/a
*** Top-posting fixed ***
James McIninch wrote:
Fao, Sean wrote:

.... snip ...

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.


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.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #16

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

Thanks,

--
Sean
Nov 14 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.