Is this program legal C89?

A

Army1987

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?
 
E

Eric Sosman

Army1987 wrote On 07/24/07 07:01,:
Is this program legal C89?

Legal. Repellent, but legal.
/* 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 type of the function pointer used at the call
site matches the type of the called function: it's legal.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Army1987 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top