Implicit int return value of C function

2019-01-03 03:16发布

I've googled and just can't seem to find the answer to this simple question.

Working on a legacy code base (ported to Linux recently, and slowly updating to a new compiler) and I see a lot of

int myfunction(...)
{
// no return...
}

I know the implicit return TYPE of a function is int, but what is the implicit return VALUE when no return is specified. I've tested and gotten 0, but that's only with gcc. Is this compiler specific or is it standard defined to 0?

EDIT: 12/2017 Adjusted accepted answer based upon it referencing a more recent version of the standard.

7条回答
疯言疯语
2楼-- · 2019-01-03 04:14

Such a thing is possible, but only under the assumption that the return value of the function is never used. The C11 standard says in para 6.9.1:

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

(AFAIR previous version of the standard had similar wording)

So it would be a good idea to transform all the functions of that sort that you have to void functions, so no user of such a function could be tempted to use the return value.

查看更多
登录 后发表回答