[Zlib-devel] exit() vs. return in main()

Nelson H. F. Beebe beebe at math.utah.edu
Thu Mar 13 06:25:01 EST 2003


Cosmin Truta <cosmin at cs.toronto.edu> writes on Wed, 12 Mar 2003 21:46:15 -0500:

>> Either choose "void main() { ... exit(0); }", or "int main() { ... return 0; }".

That is not conformant with the 1999 C Standard (p. 12): only the
latter form (int type) may be used:

>> ...
>>      5.1.2.2.1 Program startup
>>
>>      The function called at program startup is named main. The
>>      implementation declares no prototype for this function. It
>>      shall be defined with a return type of int and with no
>>      parameters:
>>
>>            int main(void) { /* ... */ }
>>
>>      or with two parameters (referred to here as argc and argv,
>>      though any names may be used, as they are local to the
>>      function in which they are declared):
>>
>>            int main(int argc, char *argv[]) { /* ... */ }
>>
>>      or equivalent;9) or in some other implementation-defined manner.
>> ...

The 1989 C Standard contains similar language.

The same syntax is required in C++: the 1998 C++ Standard says on
p. 43:

>> ...
>>       3.6.1 Main function                                          [basic.start.main]
>>
>>       A program shall contain a global function called main, which is
>>       the designated start of the program.  It is
>>       implementation-defined whether a program in a freestanding
>>       environment is required to define a main function. [Note: in a
>>       freestanding environment, start-up and termination is
>>       implementation-defined; startup contains the execution of
>>       constructors for objects of namespace scope with static storage
>>       duration; termination contains the execution of destructors for
>>       objects with static storage duration.  ]
>>
>>       An implementation shall not predefine the main function. This
>>       function shall not be overloaded. It shall have a return type of
>>       type int, but otherwise its type is implementation-defined.  All
>>       implementations shall allow both of the following definitions of
>>       main:
>>
>>              int main() { /* ... */ }
>>
>>       and
>>
>>              int main(int argc, char* argv[]) { /* ... */ }
>> ...

Also, strictly, return (0) should be replaced by return (EXIT_SUCCESS):

The 1999 C Standard says on p. 314:

>> ...
>>      7.20.4.3 The exit function
>>
>>      Synopsis
>>
>> 	    #include <stdlib.h>
>> 	    void exit(int status);
>>
>>      Description
>>      ...
>>
>>      Finally, control is returned to the host environment.  If the
>>      value of status is zero or EXIT_SUCCESS, an
>>      implementation-defined form of the status successful termination
>>      is returned. If the value of status is EXIT_FAILURE, an
>>      implementation-defined form of the status unsuccessful
>>      termination is returned. Otherwise the status returned is
>>      implementation-defined.
>>
>>      Returns
>>
>>      The exit function cannot return to its caller.
>> ...

The 1998 C++ Standard says on p. 336,

>> ...
>>      18.3  Start and termination               [lib.support.start.term]
>> ...
>> 	    exit(int status)
>> ...
>>       -- Finally, control is returned to the host environment.  If
>> 	 status is zero or EXIT_SUCCESS, an implementation-defined
>> 	 form of the status successful termination is returned. If
>> 	 status is EXIT_FAILURE, an implementation-defined form of the
>> 	 status unsuccessful termination is returned.  Otherwise the
>> 	 status returned is implementation-defined.
>> ...

The exit value 0 was historic on Unix, but other operating systems
have had different conventions, so EXIT_SUCCESS and EXIT_FAILURE were
introduced into the C and C++ Standards for O/S portability.  They
also give an obvious name to an otherwise-magic constant.


-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- Center for Scientific Computing       FAX: +1 801 581 4148                  -
- University of Utah                    Internet e-mail: beebe at math.utah.edu  -
- Department of Mathematics, 110 LCB        beebe at acm.org  beebe at computer.org -
- 155 S 1400 E RM 233                       beebe at ieee.org                    -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe  -
-------------------------------------------------------------------------------




More information about the Zlib-devel mailing list