Must main return a value?

P E Smee exspes at gdr.bath.ac.uk
Sat Jun 29 00:19:49 AEST 1991


In article <7830001 at hpwrce.HP.COM> walterm at hpwrce.HP.COM (Walter Murray) writes:
>Is the following program strictly conforming?
>
>   int main(void) { return; }
>
>Many people have written that the main function must return a value.
>I can't find such a requirement in the Standard.  I have read 2.1.2.2.3
>and 3.6.6.4.  Am I missing something?

Yep.  The construct you used, 'int main (...' declares main to be a
function which returns an int.  You have declared it as returning a
value, and then not returned a value of the type specified.  Speaking
pedantically, that's a prima facie violation.

In practice, since return from main() generally throws you out of your
C-program environment, and back into your native operating system
environment, whether or not it makes any difference depends on your
OS.  Many OSes expect progs to return status codes -- especially progs
which have been declared to return status codes.  If you don't return
one, the results will be OS-dependent but can range the full gamut from
unpleasantly surprising to unpleasantly mysterious.

If you're really unlucky, your failure to return the value you promised
won't make any difference at all -- until next year, when you've got to
move the prog to another OS platform and have forgotten what you did.

-- 
Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK
 P.Smee at bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132



More information about the Comp.std.c mailing list