Nick said:
Thanks for confirming it. It's odd that an int function that ends with
"return;}" is a constraint violation but one that ends with a plain "}"
isn't, since for a void function they'd both be OK.
Yes, it's odd, but it's easier for a compiler to determine that a
return with no expression in a non-void function, or a return with
an expression in a void function, is an error, than to detect a
*missing* return statement.
For example:
int spin(void) {
while (1) {
continue;
}
}
The missing return isn't really an error, since the closing brace can
never be reached.
Also;
int foo(void) {
if (condition) {
return 42;
}
}
This isn't an error if "condition" is always true.
Reaching the closing "}" of a non-void function is really a run-time
error that can sometimes, but not always, be detected at compile
time.
The standard *could* have required that no execution path in a
non-void function may reach the closing "}", but that would require
more analysis than the standard generally requires compilers to
perform. Compilers are of course free to warn about missing returns.
Something I never noticed before: as far as I can tell, this:
int func(void) {
struct { double x; } result = { 0.0 };
return result;
}
does not violate any constraints. Its behavior is undefined, but
only by an omission of the definition of the behavior. C99 6.8.6.4p3
says:
If the expression has a type different from the return type of
the function in which it appears, the value is converted as if by
assignment to an object having the return type of the function.
An assignment won't convert from a struct type to int, so there's no
definition of the behavior. Was this an oversight?
Followups redirected to comp.lang.c; this is probably beyond what's
topical for comp.lang.java.programmer.