Can a void-returning function g return f(); when f returns void?
c, c11, language-lawyer
Solution
Anything after `return` is an expression.
6.8.6:1 Jump statements
Syntax
...
return expressionopt;
And standard says that:
A return statement with an expression shall not appear in a function whose return type is void. ....
`f()` is also an expression here. The compiler should raise a warning
[Warning] ISO C forbids 'return' with expression, in function returning void [-pedantic]
Problem
Consider the following snippet: ``` void f(void); void g(…) { … return f(); … } ``` Is this `return f();` valid according to C11? I am not advocating using this pattern: if it works at all, it is obviously equivalent to `f(); return;` (where the `return;` itself would be redundant if this is at the end of function `g()`). I am asking this question in the context of the static analysis of C programs, where the C code has already been written by someone else and the question is deciding whether or not it is valid according to the standard. I would interpret C11 6.8.6.4:1 as meaning that it is non-standard and should be statically rejected. Is it possible to interpret it differently (I have found this pattern in actual and otherwise high-quality source code)? Constraints A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.