Path: utzoo!utgpu!water!watmath!clyde!rutgers!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: <176@fxgrp.UUCP>
Date: 16 Dec 87 21:10:51 GMT
References: <10875@brl-adm.ARPA>
Reply-To: fxgrp!ljz@ames.arpa (Lloyd Zusman, Master Byte Software)
Followup-To: <10875@brl-adm.ARPA> ADLER1%BRANDEIS.BITNET@CUNYVM.CUNY.EDU
Organization: FX Development Group, Inc., Mountain View, CA
Lines: 62

In article <10875@brl-adm.ARPA> ADLER1%BRANDEIS.BITNET@CUNYVM.CUNY.EDU writes:
>I was looking through the file crt0.c in the GNU emacs source code and
>found the command
>
>exit(main(argc,argv,env));
>
>which I find puzzling. I thought that one was supposed to give exit a
>number for an argument. What does the above command do and why would
>anyone want to do it that way ?


This command invokes your main() routine with the proper arguments.  Just
like any other C function, main() returns a value.  Consider the following
code fragment ...

    int retcode;
          .
    	  .
    	  .
    retcode = main(argc, argv, env);
    exit(retcode);

In this case, exit() is getting a number as its argument.  This is equivalent
to the "exit(main(argc, argv, env))" command that was mentioned above.

Perhaps your confusion is due to the fact that you didn't think that main()
is the same as other functions.  It is.  You can give it a return()
statement ...

    main()
    {
    	    .
    	    .
    	    .
    	return(99);
    }

This is supposed to be equivalent to

    main()
    {
    	    .
    	    .
    	    .
    	exit(99);
    }

I say "supposed to" because I've used C compilers on the IBM PC where
this doesn't work.

The "exit(main(...))" construct in the crt0.c will ensure that this
construct works like it should.





-------------------------------------------------------------------------
 Lloyd Zusman
 Master Byte Software
 Los Gatos, California	    	    	Internet:   fxgrp!ljz@ames.arpa
 "We take things well in hand."	    	UUCP:	    ...!ames!fxgrp!ljz