Return vs. exit in main() [WAS Re: "exit 0;" - or - "exit (0);" ?]
Matt Landau
mlandau at Diamond.BBN.COM
Sat Nov 22 03:40:11 AEST 1986
In article <1556 at batcomputer.tn.cornell.edu> braner at batcomputer.UUCP (braner)
writes:
>Actually, I now think that what I've seen was "return n;" in main(),
>which I guess has the same effect as exit(n); - or does it?
It is implementation-dependent whether return(x) and exit(x) are equivilent
from within main(). In most (all?) compilers, there is a startup routine
that calls the user's main; nowhere is it cast in stone what this startup
routine does when main returns a value to it. I know of compilers that fit
each of the following descriptions:
- startup routine expects main to be "void main", and does
not accept a return value, but always returns 0 to the
operating system upon completion
- startup routine expects main to be "int main", but ignores
any value returned by main and always returns 0 to the OS
- startup routine uses the return value of main as its own
return value to the OS
In the last case, return and exit are equivilient; in the other two cases,
you must use exit(x) to exit with some status other than 0.
Does the most recent ANSI draft have anything to say on this subject?
--
Matt Landau BBN Laboratories, Inc.
mlandau at diamond.bbn.com 10 Moulton Street, Cambridge MA 02238
...seismo!diamond.bbn.com!mlandau (617) 497-2429
More information about the Comp.lang.c
mailing list