[comp.lang.c] Return vs. exit in main

mlandau@Diamond.BBN.COM (Matt Landau) (11/21/86)

In article <1556@batcomputer.tn.cornell.edu> braner@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@diamond.bbn.com		10 Moulton Street, Cambridge MA 02238
 ...seismo!diamond.bbn.com!mlandau      (617) 497-2429

gwyn@brl-smoke.ARPA (Doug Gwyn ) (11/22/86)

In article <2330@diamond.Diamond.BBN.COM> mlandau@Diamond.BBN.COM (Matt Landau) writes:
>Does the most recent ANSI draft have anything to say on this subject?

Yes.  main() is an int-valued function; its return value is used the same
way as it would have been if termination were achieved by invoking exit()
with the same value as a parameter.  If you don't return anything, the
termination status is undefined, as one would expect.