Re: Conformance to standards,using exit() or return from main() correctly

From: Peter Ajamian (peter@pajamian.dhs.org)
Date: 01/07/01


George Greer wrote:
>
> On Sun, 7 Jan 2001, Peter Ajamian wrote:
>
> >[...] Anyways, my understanding of the standard is that main can only
> >return one of three values, those being 0 (to indicate success),
> >EXIT_SUCCESS (which is #defined to 0 in stdlib.h), or EXIT_FAILURE (which
> >is #defined to some non-zero value in stdlib.h).  An implementation can
> >make use of other return values but does not have to and any other return
> >values do not necessarily indicate success or failure. [...]
> >
> >Now after noticing a few instances where exit(1) is apparently used (in
> >all likelihood incorrectly) to indicate failure I did the following from
> >the shell...
>
> In most programs:
> !0 is failure
>  0 is success

I have no arguments that using exit(1) will work in almost every
concievable case, however the standard gives us an easy method to use
that is guaranteed to work in every case, why not use it?  To not use it
is setting ourselves up for failure when we filnally do encounter that
one implementation that decides to take the standard for it's word and
use some value for failure other than 1 (and decides to use a return
value of 1 to do something totally rediculous).  It's pretty much boils
down to this...

exit(1);
Will work in nearly every case but is not guaranteed to work and is (to
the best of my knowledge) undefined which means that a platform or
implementation can pretty much do what it wants with this result and
does not have to document it.

exit(EXIT_FAILURE);
Guaranteed under the ANSI/ISO standard to always indicate a failure to
the OS.

Note that exit(0) is valid under that standard and is fine to use
(although I generally prefer to use EXIT_SUCCESS for the sake of
clarity).

Regards, Peter

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/03/01 PST