473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

main return codes

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
16 3907
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
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
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
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
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
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
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


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
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
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
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
<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
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
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
*** 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: martinnitram | last post by:
Dear all, Following are some codes: from myClass import * # some user define classes, which will catch the exception within its function thread_function(): myClass myclass while (1):
192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
3
by: Grant Schoep | last post by:
Ok, back to basics here. I was completly shocked and dismayed today when one of my apps returned an error from main. Not so much that it returned an error, that was expected. But what happened....
6
by: Josh Mcfarlane | last post by:
I keep trying to get myself out of the return-code mindset, but it doesn't seem to work. They are suppose to get rid of if-then statements of return codes, but you still have to do an if statement...
3
by: c# beginner | last post by:
we are trying to standardize return codes across our .NET applications (that are soon to be developed.) What is the best practice for standardizing return codes? I know of only the following...
4
by: Ben | last post by:
could someone please suggest to me another way to work around sub main? I am currently migrating 6.0 codes over to .net. as soon as sub main() ran, vb ends the application immediately. So I...
5
by: # include | last post by:
what is the ((((return any no;))) in the main mean plz Ex. int main() { ; ; ; return ??;
9
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
29
by: pereges | last post by:
By main file, I mean the one which contains the main routine. Can some one please provide suggestions as to how I can improve the organization of main file ? I have just though about a rough...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.