Mail Archives: djgpp/2000/10/25/05:46:19
Mike Wahler wrote:
>
> Russell Wallace <rwallace AT esatclear DOT ie> wrote in message
> news:39f5ff14 DOT 90960240 AT news DOT esatclear DOT ie...
> > On Mon, 16 Oct 2000 07:45:31 +0100, Richard Heathfield
> > <binary AT eton DOT powernet DOT co DOT uk> wrote:
> > >Schildt is probably the primary reason that so many C programmers think
> > >exit(1) is portable.
> >
> > Don't think I've read any of Schildt's books, but I'll admit I always
> > did think exit(1) was portable. Why isn't it, and what should be used
> > instead?
>
> The only return (exit) values defined by the standard are:
>
> a) EXIT_SUCCESS
> b) EXIT_FAILURE
> c) The integer value zero (0)
>
> The 'EXIT_' macros are defined in
> <stdlib.h>
Exactly right. To clarify a bit for the OP, though: exit(1) is portable
in the sense that it will always return 1 to the calling process (just
like return 1; in main(), to which this discussion applies also!); what
is /not/ portable is the /semantics/ - i.e. what the code /means/.
Different return codes mean different things to different operating
systems. For example, there's one OS that interprets even return codes
as failures, and odd return codes as successes! (Is it VMS?)
Now, if you return 0 or EXIT_SUCCESS from your program, either via a
return from main() or from exit(), a conforming C compiler guarantees
that it will translate that value into the value interpreted by the OS
as "success". If you return EXIT_FAILURE, the compiler guarantees that
it will translate /that/ value into the value interpreted by the OS as
"failure". Thus, in the case of VMS(?), the compiler will translate
"return 0;" into "return some odd number or other" for you, and "return
EXIT_FAILURE;" into "return some odd number or other" (and,
interestingly, it need not be the /same/ odd number as for "return 0"!
(although it probably will be, I guess)). Similarly, a VMS(?) C compiler
would ensure that "return EXIT_FAILURE" returned an even number to the
OS.
No other values than the ones cited are semantically portable.
I hope that clarifies matters for the OP.
--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html
- Raw text -