Return void type in C and C++

前端 未结 5 1379
我在风中等你
我在风中等你 2020-12-08 13:11

This compiles without any warnings.

Is this legal in C and C++ or does it just work in gcc and clang?

If it is legal, is it some new thing after C99?

5条回答
  •  庸人自扰
    2020-12-08 13:59

    Standard C does not support this construction:

    C11 6.8.6.4: The return statement

    Constraints

    1 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.

    No special provisions are added for the special case in the question. Some C compilers do support this as an extension (gcc does, unless instructed to conform to one of the C Standards), but C11 and previous versions consider it a constraint violation.

提交回复
热议问题