[comp.lang.c] return-from-main vs exit

chris@mimsy.UUCP (Chris Torek) (12/27/87)

In article <454@viper.Lynx.MN.Org> john@viper.Lynx.MN.Org (John Stanley)
writes the following concerning the equivalence of return-from-main and exit:
>In theory: maybe.  In practice: not-on-a-bet....!

True.  To make programs work on improper implementations, one must use
exit(e) rather than a return(e) from main.

>  There are an abundance of existing programs that -do-NOT- return 
>any value from main.  They just terminate.

Or in other words, there are an abundance of buggy programs in existence.
(But we knew that already :-) .)

>... in this situation, poor style isn't really relevant...they exist and
>part of the objective for the ANS standard is to not break existing code...  

Close.  That objective is not to break existing *correct* code.  It
says nothing about existing *incorrect* code.

>(Examples of this style are in K&R and must be considered part of the
>baseline definition of how many people have been writing main() 
>functions for years.)

Look up `exit' in the index, and read the description to which it
points.  I believe it notes that return-from-main is equivalent,
and that one or the other is required.  This is one of a very few
complaints I have with K&R, that they leave this until near the
end of the book.  (This was not an accident; I just happen to
disagree with the reasoning behind it.)

>If the ANS standard does not deal with this problem, it
>should do so before being released.

It deals with it quite well, requiring that the value returned from
main (if main ever returns) be passed on to the invoking environment
in a hosted implementation.  Nonetheless,

>[using exit is] a necessity for anyone who has to
>write code that needs to be portable

which I would complete by adding, `to broken but otherwise useful
implementations.'
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris