“missing return statement”, but I know it is there

前端 未结 4 993
面向向阳花
面向向阳花 2021-01-06 19:24

Assume I have the following function:

// Precondition:  foo is \'0\' or \'MAGIC_NUMBER_4711\'
// Returns:       -1 if foo is \'0\'
//                 1 if fo         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 19:55

    In C89 and in C99, the return statement is never required. Even if it is a function that has a return different than void.

    C99 only says:

    (C99, 6.9.1p12 "If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined."

    In C++11, the Standard says:

    (C++11, 6.6.3p2) "Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function"

提交回复
热议问题