On 2008-07-23 04:26:11 -0400, James Kanze <
[email protected]> said:
As far as I can see, both languages require the same thing
here. I don't have the C90 standard handy, but C99 says that
main "shall be defined with a return type of int ...".
Similarly, in C++, main "shall have a return type of int ...".
In both cases, a return type of void is a valid extension: a
conforming compiler must issue a diagnostic, and having done
that, it can do whatever its implementor chooses.
There's a subtle difference in the way the standards are worded.
In C++, an error requires a diagnostic unless it is specified to
be undefined behavior; in C, an error is undefined behavior
unless it is specified to require a diagnostic. Beyond that,
of course, there are some general statements: violating a
constraint on an operator in an expression requires a diagnostic
in C, because there is a general statement which says so, even
if there isn't a specific statement for each constraint. And in
both languages, if the standard neglects to specify the
behavior, it is undefined. But as far as I can tell, none of
these cases affect the return value of main.
Note too the difference in wording concerning main. In C++: "It
shall have a return type of type int, but otherwise its type is
implementation-defined." (The "otherwise" is, IMHO,
significant.) In C: "[...] or in some other
implementation-defined manner." In context, it's not really
clear if that statement applies just to the parameters, or to
the function signature in general. I'd interpret it as
applying to the function signature in general, but I can easily
understand the other interpretation as well. Regardless,
however; C doesn't require a diagnostic if you define main with
a signature which isn't supported; C++ does.