Unexpected error when compiling for ANSI C89

Hi CFE-Devs,

When I compile the following tiny test with ‘-std=c89’ (‘clang –std=c89 –c xxx.c’):

foo(x) {

return;

}

I get the following error message:

xxx.c:2:3: error: non-void function ‘foo’ should return a value [-Wreturn-type]

return;

^

1 error generated.

But for ANSI C89, this is legal, and it is a really common pattern to have a return with no value, even though the return type is implicitly ‘int’, especially when K&R syntax is used:

Section 3.6.6.4. The return statement, Semantics, paragraph 3

If a return statement without an expression is executed, and the value of the function call is used by the caller, the behaviour is undefined. reaching the } that terminates a function is equivalent to executing a return statement without an expression.

Yet the following results in a warning and not an error:

foo(x) {

}

I get the following perfectly reasonable warning:

xxx.c:2:1: warning: control reaches end of non-void function [-Wreturn-type]

}

^

1 warning generated.

When compiling for ANSI C89, I think that the first example should also result in a warning and not an error.

Thanks,

MartinO

Looks like an oversight in ; in C99, we want to default to an error, but C89 we shouldn’t, as you’ve pointed out. See also . That said, it’s been like this for seven years… it seems like this isn’t causing issues in practice. -Eli

Haha, 7 years. I tried with v4.0 and it was there too, but I did not look further back. Yes, the test case is contrived, but part of ANSI C89 validation. I am not aware of any real-world-code that is still around that depends on this though.

All the best,

MartinO