Why does a “function name” evaluate to true in C and how to get warned on it

前端 未结 5 2109
粉色の甜心
粉色の甜心 2020-12-31 11:14

I recently stumbled across the following behaviour of gcc 3.2.2 writing a c program:

In an if statement I forgot the braces of a function and wrote:

if

5条回答
  •  时光说笑
    2020-12-31 11:58

    if (myFunc) is equivalent to if (&myFunc), so you're testing the address of a function, which of course will always be non-zero, i.e. true.

    With gcc 4.2.1 and -Wall I get the following warning:

    myfunc.c:11: warning: the address of ‘myFunc’ will always evaluate as ‘true’

提交回复
热议问题