Subject: Re: main return...
To: None <current-users@NetBSD.ORG, waldi@moacs.indiv.nl.net>
From: Peter Seebach <seebs@solon.com>
List: current-users
Date: 03/25/1996 19:01:04
No, there are *not* cases in which it is all right; it is invalid
standard C. If you declare main to return int, but return no
value, the termination status is undefined; if you declare it to
return void, the entire program is undefined. (This is probably
for the benefit of machines where different return types involve
different stack setup.)
However, even if you never return, main must have a return type of int
in Standard C.
Several books get this wrong.
I do not object to a silent 0 exit status; this is explicitly allowed
for. I do mildly object to gcc suppressing the warning, but it
could easily be added back in.
I would rather have a warning for "void main" or "struct foo {} main"
(a common typo mistake) than have a warning for falling off the end;
a warning for specifically illegal code is better than a warning for
specifically legal code.
If anyone wants the specific quotes and references on why void main
is forbidden, not merely bad style, I'd be glad to quote the standard,
it's just a bit dry as reading goes. Quick summary: The behavior
of main is defined for two forms, int main(void), and int main(int argc,
char *argv[]). Wording is provided that allows argv to be spelt "char **",
and either parameter to have different names. No other forms are
defined; thus, by 3.16 (ISO numbering), any other form introduces
undefined behavior. Specific quotes available by request.
-s