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

D'Arcy J.M. Cain darcy at druid.uucp
Thu Jul 5 23:31:31 AEST 1990


In article <833 at mwtech.UUCP> martin at mwtech.UUCP (Martin Weitzel) writes:
>Nearly right! I'll never forget the day when I tracked down what
>seemed to be a really wierd problem in some printer driving code
>to the following (still buggy!) program:
>
>	#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 didn't say but I assume that the problem was garbage output at the
end of the program?  Other ways to fix this:
    declare buffer as static
    flush buffers before returning
    drop the setbuf call

The same sort of thing could happen if you malloc space for the buffer and
then free it when you are finished doing output.

-- 
D'Arcy J.M. Cain (darcy at druid)     |   Government:
D'Arcy Cain Consulting             |   Organized crime with an attitude
West Hill, Ontario, Canada         |
(416) 281-6094                     |



More information about the Comp.lang.c mailing list