Army1987 wrote:
Is this program legal C89?
/* no headers included */
int main(void)
{
if (sizeof (exit(0), 0),
((void (* )(int))&exit)(
(puts((const char *)"hello, world"), 0)
),
0) {
}
}
The the first operand of the first comma serves to implicitly
declare exit() as returning an int, but it is never called as
such. Does that wrong implicit declaration cause UB by itself,
even if the function is eventually called with the right type?
The only relevant paragraph in a C89 draft I'm looking at is
3.1.2.6 Compatible type and composite type
"All declarations that refer to the same object or function shall have
compatible type; otherwise the behavior is undefined."
Considering the implementation of the standard library contains a definition
(which is a declaration) of exit(), and your program's implicit declaration
is not compatible with it, I believe the behaviour is undefined, but
whether 3.1.2.6 is meant to apply to standard library functions is not
entirely clear. C99 clears it up with an explicit prohibition against
incorrect declarations of standard library functions:
7.1.3p2:
"No other identifiers are reserved. If the program declares or defines an
identifier in a context in which it is reserved (other than as allowed by
7.1.4), or defines a reserved identifier as a macro name, the behavior is
undefined."
so if you make the implicit declarations explicit, it's still definitely
invalid C99, but I have been told this text is not present in C89.