Exit from main

Brendan J. McMahon bjm at sabin.UUCP
Wed Aug 17 02:43:27 AEST 1988


Fragment from a recent posting:
>	#include <stdio.h>
>	main() {
>		char buf[100];
>		(void) fgets(buf, sizeof buf, stdin);
>		(void) fputs(buf, stdout);
>		exit(0);
                ^^^^
>	}

Different topic.  Is there any reason why you should have to call exit,
at this point in a program besides buffer flushing, or returning a value?  
I just had one of the most frustrating bugs that I had in a while.
On a XENIX 386 5.2a I had a program that went something like:
   #include <stdio.h>
   main() {
   void compare();
           puts("pre compare call"); /* only added after much debuging */
           compare();
           puts("post compare call"); /* ditto */
   }
output:
pre compare call
(output from compare function)
post compare call
pre compare call
(output from compare function)
post compare call
pre compare call
( on and on until a core dump ) 

I compiled the program on a Berkley 4.something Vax and it worked fine.
Adding an exit(0); after the post compare call on XENIX fixed it (forced term-
ination).  Any ideas why it didn't exit by itself? 

--
Brendan J. McMahon
Sabin Metal Corp. |     Refiners of Precious Metals    | Hardware Trouble?
Scottsville, NY   |  ****** Au  Ag  Pt  Pd  Rh ******  | Give us a call, we'll
716-538-2194      |lazlo!sabin!bjm  || ritcsh!sabin!bjm| melt your trouble away



More information about the Comp.unix.wizards mailing list