From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
Newsgroups: comp.lang.c
Subject: Re: main()
Date: 4 Sep 92 19:13:47 GMT
Organization: University of Helsinki

mjn@pseudo.uucp (Murray Nesbitt) writes:
>Functions do not have to explicitly return values.  What's the return
>value of ``free()''?  Of ``srand()''?  Of ``rewind()''?  Are you
>telling me I must rewrite my ``void spinthewheel( wheel *w )''
>function?

Ouch.  I obviously forgot void value functions.  If I could go back in
time and edit what I wrote, I'd probably come up with something like
this:

	Each and every function (that returns at all and does not have
	the return type void) needs to explicitly return some value,
	otherwise it will return garbage value and cause your hair turn
	into worms. 

Better?

>No operating system I have ever encountered will get confused by a
>program that returns an arbitrary value.

VAX/VMS comes to mind: for some exit codes it prints (confusing,
erroneous, and misleading) error messages.  For UNIX and MS-DOS, the OS
itself doesn't get particularly confused, but the user of the program
often does.

>See if you can confuse your operating system with the following:
>[ program that was essentially return(rand()) deleted ]

Yup, I got the expected results.  Error messages from VAX/VMS, or make
under Unix or DOS.

>>Of course, if you terminate your program with exit or abort, it will
>>never get to the end of main, so in that case there is no need to
>>include a return statement there.
>
>Personally, I put a ``return'' statement in regardless, with a 
>``/* NOTREACHED */'' if appropriate.

A stylistic issue, of course, but I use simply:

	int main(void) {
		...
		exit(0);
	}

I do not think that there is anything to be won by putting extra stuff
after the exit.  (I know it may silence lint or the compiler, but I
prefer to fix them instead.  With gcc this isn't a problem.  I freely
admit that this doesn't work for all people.)

[* If you want to argue this point, it would probably be best done in
email.  Style wars are not usually fruitful in newsgroups.  I'll
summarize if anything interesting comes up. *]

>You should be using ``exit( EXIT_FAILURE )'' or ``exit( EXIT_SUCCESS )''.

exit(0) and exit(EXIT_SUCCESS) are defined to be exactly equivalent, at
least according to Plauger & Brodie's "Standard C" (I do not, alas, have
access to a copy of the standard).  I seem to remember that
exit(EXIT_SUCCESS) and exit(1) were similarly defined to be equivalent,
but I can't seem to locate the reference at this time.  I could
misremember.  I hope not, but unless somebody (or I) can find an actual
reference, I'll trust you on this one.

>What does exit(0) mean to VMS?

The same as ``exit(EXIT_SUCCESS)'' I'd hope (and I think this was stated
as being a fact some time ago when this was last discussed; I'd check
but after testing your program that exited with random return values,
I've had quite enough of VMS for next few months -- yeah, I'm easily
annoyed by it), although since VAX/VMS C isn't standard conforming (last
I heard, anyway), it could mean anything. 

[ I claimed that books advocating ``void main'' were suitable for
  "heating purposes" ]
>I hope this doesn't include my copy of K&R, in which most example
>programs have no explicit return value from main().  This is
>functionally equivalent to ``void main()''.

The lack of functional equivalence was covered by another poster
already, but I'll add that K&R is unfortunately not exactly the best
reference when it comes to program correctness or portability advice.  I
have abandonded it as a reference, in favor of Plauger & Brodie's
"Standard C".  K&R is a fairly good book, but it contains too many small
imperfections to be considered part of the small elite of extraordinary
good C books.  It's claim for fame depends historical reasons and on
being written very well (few books are pleasant to read, K&R is). 

--
Lars.Wirzenius@helsinki.fi
Newsgroups: comp.lang.c