Can a void-returning function g return f(); when f returns void?

后端 未结 3 1226
不知归路
不知归路 2020-12-30 23:26

Consider the following snippet:

void f(void);

void g(…)
{
  …
  return f();
  …
}

Is this return f(); valid according to C11?

3条回答
  •  盖世英雄少女心
    2020-12-31 00:14

    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]
    

提交回复
热议问题