[comp.lang.c] is there anything wrong with return

friedl@vsi.UUCP (Stephen J. Friedl) (07/01/88)

Hi net.folks,

     For some unknown reason, I have always felt uncomfortable
doing a return(n) from main: I used exit(n) instead.  Can anybody
think of any possible reason why a return is not a good idea?  I
have the feeling that my discomfort is entirely unfounded...

     Steve
-- 
Steve Friedl     V-Systems, Inc. (714) 545-6442     3B2-kind-of-guy
friedl@vsi.com     {backbones}!vsi.com!friedl    attmail!vsi!friedl

Nancy Reagan on John DeLorean: "Just say snow"

guy@gorodish.Sun.COM (Guy Harris) (07/01/88)

>      For some unknown reason, I have always felt uncomfortable
> doing a return(n) from main: I used exit(n) instead.  Can anybody
> think of any possible reason why a return is not a good idea?

Some implementations don't properly implement "return(n)" from "main".  SunOS's
C implementations did not do so from SunOS 2.0 to SunOS 3.5; they implement it
correctly in 4.0.  I seem to remember somebody claiming some other
implementation did not do so.

I think every S5 implementation from AT&T handled "return(n)" from "main"
correctly, as did 4.[23]BSD; this does not, of course, guarantee that any
particular implementation derived from either or both of those will do so
correctly.

ANSI C mandates it, so everybody's going to have to get with the program
eventually.

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (07/01/88)

In article <741@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes:
| Hi net.folks,
| 
|      For some unknown reason, I have always felt uncomfortable
| doing a return(n) from main: I used exit(n) instead.  Can anybody

  Some versions of C don't use the value of the return from main as a
call to exit. Instead, the assume that if you don't call exit with a
value you terminated normally and should have a zero exit status. Your
gut feeling is a good one, it will save you a lot of pain in the long
run, I suspect.

-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

gwyn@brl-smoke.ARPA (Doug Gwyn ) (07/02/88)

In article <741@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes:
>doing a return(n) from main: I used exit(n) instead.

"return n" is technically correct, and is slightly more satisfactory
from a "top-down control" viewpoint.  I have heard that there are
some purported C implementations that ignore the return value
from main(), though, always returning a success indicator unless
exit() is used.  Those aren't Standard-conforming implementations,
but you might be stuck with one.

P.S.  You don't need parentheses around the return expression.

bill@proxftl.UUCP (T. William Wells) (07/04/88)

In article <741@vsi.UUCP>, friedl@vsi.UUCP (Stephen J. Friedl) writes:
>      For some unknown reason, I have always felt uncomfortable
> doing a return(n) from main: I used exit(n) instead.  Can anybody
> think of any possible reason why a return is not a good idea?  I
> have the feeling that my discomfort is entirely unfounded...

The only reason I can think of is broken compilers. The two
should be equivalent. Of course, one of those broken compilers
is the one we have ... on a Sun 3 running SunOS 3.4. It acts as
if return (n) were a return (0). [I understand that that
has been fixed in a later release.]

ckl@uwbln.UUCP (Christoph Kuenkel) (07/15/88)

In article <741@vsi.UUCP>, friedl@vsi.UUCP (Stephen J. Friedl) writes:
>      For some unknown reason, I have always felt uncomfortable
> doing a return(n) from main: I used exit(n) instead.  Can anybody
> think of any possible reason why a return is not a good idea? 
There are simply some implementations of _startup() that don't take care of
the returnvalue of main() (we have that problem here). You should code:
main() {
	...
	exit(retcode);
	/*NOTREACHED*/
}
This should work in any case.
christoph
-- 
Christoph Kuenkel/UniWare         ck@tub.BITNET
Kantstr. 152                      uunet!unido!tub!ck
1000 Berlin 12                    {unido,tmpmbx,uwnue,nixbln,tub}!uwbln!ckl
West Germany