Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!umd5!ames!fxgrp!ljz From: ljz@fxgrp.UUCP (Lloyd Zusman, Master Byte Software) Newsgroups: comp.lang.c Subject: Re: exit(main(argc,argv,env)); Message-ID: <182@fxgrp.UUCP> Date: 18 Dec 87 20:29:25 GMT References: <10875@brl-adm.ARPA> <1451@houdi.UUCP> Reply-To: fxgrp!ljz@ames.arpa (Lloyd Zusman, Master Byte Software) Followup-To: <1451@houdi.UUCP> marty1@houdi.UUCP (M.BRILLIANT) Organization: FX Development Group, Inc., Mountain View, CA Lines: 67 In article <1451@houdi.UUCP> marty1@houdi.UUCP (M.BRILLIANT) writes: >> I was looking through the file crt0.c in the GNU emacs source code and >> found the command >> >> exit(main(argc,argv,env)); > ... >The key question is where the exit(main(..)) was found. Since main() >is the first function called, no statement is needed to invoke main(). >Put it another way, since main() is invoked anyway, any statement that >calls main() must call it recursively. Why would anybody do that? It is a de facto and possibly also de jure standard in unix for the run-time support code for a C program exist in a file called crt0.o, whose source code would, presumably, be in a file called crt0.c. By "run time support" I mean the following: the code in crt0.o is what gets executed *before* the main() routine gets called. This code will do things such as intialize certain C-library variables, open stdin, stdout, and stderr, parse command-line arguments, and possibly many other things. One of the final things it does is to invoke a function called "main" with the argc, argv, and possibly also envp arguments. This is how the system knows to start executing your program at the main() entry point. The linker knows about this crt0.o file and automatically links it in without you having to specify it. Hence, it is entirely appropriate for an the above-mentioned exit statement to appear in crt0.c. With regard to the issue of main() having a return statement, if you use the crt0.c startup module (i.e., runtime support module) described above, you can have your main() routine issue a return(N) statement and it will be treated just as if you issued an exit(N) statement (for integer values of N, of course). I'm not sure if this is standard behavior. In several C compilers on the IBM PC, there is a varied interpretation of this. I've seen the following two ways of doing this in the runtime support modules of IBM PC C compilers. (1) As above. In other words, exit(main(argc, argv, envp)); (2) Ignore main's return code main(argc, argv, envp); exit(0); The first option will behave as described above with regard to return and exit in main(). The second one requires the programmer to put an exit(N) statement into the main() routine if an non-zero exit code is desired. Any return statements in the main() routine would have their arguments ignored in the second option. A disadvantage of the first approach is that you will get an indeterminate exit code if you leave the main() routine without a return or an exit, which is a quite common occurrence. I'm sure that we could have a nice long discussion about this issue. ------------------------------------------------------------------------- Lloyd Zusman Master Byte Software Los Gatos, California Internet: fxgrp!ljz@ames.arpa "We take things well in hand." UUCP: ...!ames!fxgrp!ljz