Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

后端 未结 9 2378
粉色の甜心
粉色の甜心 2020-11-21 07:37

Ever since I realized many years ago, that this doesn\'t produce an error by default (in GCC at least), I\'ve always wondered why?

I understand that you can issue co

9条回答
  •  面向向阳花
    2020-11-21 08:03

    It is a constraint violation in c99, but not in c89. Contrast:

    c89:

    3.6.6.4 The return statement

    Constraints

    A return statement with an expression shall not appear in a function whose return type is void .

    c99:

    6.8.6.4 The return statement

    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.

    Even in --std=c99 mode, gcc will only throw a warning (although without needing to enable additional -W flags, as is required by default or in c89/90).

    Edit to add that in c89, "reaching the } that terminates a function is equivalent to executing a return statement without an expression" (3.6.6.4). However, in c99 the behavior is undefined (6.9.1).

提交回复
热议问题