return in main is *not* equivalent to exit() (was Re: main() arguments)

Edward Birch edward at runxtsa.runx.oz.au
Wed Jul 11 13:15:15 AEST 1990



>	#include <stdio.h>
>	int main(argc, argv)
>		int argc; char **argv; /* OK so, Chris? :-) */
>	{
>		char buffer[BUFSIZ];
>		setbuf(stdout, buffer);
>		........ /* some code which produced output */
>		return 0;
>	}
>
>Note that the bug could be eliminated by replacing `return 0;'
>with `exit (0);'. Well, after that the cause for this problem
>became obvious to me ... of course, all of you readers out there
>in net-world allready know what the problem is, don't you :-)

You will find that the real problem is that some how the return address
for main has been corrupted.

As far as I am concerned return <expr> and exit(<expr>); are the same
thing for "main", in the general case.

Please note that "main" is a routine in it's own right and can be called
recursively! Hence in the recurive case exit(<expr>) is explicit.
It means terminate now, whereas return <expr>; may not.
I think this is what K&R meant at the time.

Secondly "main" as the first routine of a program is only a convention
most compilers have some other entry point like "start" which sets up
the arguments and environment variables etc for "main" and then calls
"main" the object code  for these modules is stored in /lib/crt0.o .

Edward Birch

Telex: 10713856 BRHT

UUCP:   seismo!munnari!runx.oz!edward   ACSnet:  edward at runx.oz
ARPA:   edward%runx.oz at seismo.css.gov   CSNET:   edward at runx.oz



More information about the Comp.lang.c mailing list