main return value

cschmidt at lynx.northeastern.edu cschmidt at lynx.northeastern.edu
Fri Jun 7 03:05:57 AEST 1991


Thanks to all who posted or mailed replies to my question about the
"main" function.  Sorry for being so tardy in posting this summary.

The question was, what normal (ie non-error) value should main return
in an ANSI C program?  The answer is implied by the following two
rules about ANSI C.  I am grateful to those who pointed out these
rules.  Eventually, I found these rules on page 106 in the book
"Standard C" by Plauger and Brodie.

(1) The main function should be declared as returning int.

(2) The statement "return x;" in the body of main is equivalent to
    "exit (x);".

This then is the correct form:

    #include <stdlib.h>     /* defines EXIT_SUCCESS */
    int main (...)
    { ... return EXIT_SUCCESS; }

This issue arose when I was porting some programs to VAX C, which it
turns out is not ANSI.  VAX C violates rule (2) above.  Thanks to
those of you who suggested workarounds.

It is almost always a programming error when a function declared as
returning a value has no "return" statement.  Such a function is not
what I would call lint-free.

Christopher Schmidt
cschmidt at lynx.northeastern.edu



More information about the Comp.lang.c mailing list