Why is an empty string literal treated as true?

后端 未结 3 753
长发绾君心
长发绾君心 2021-01-07 17:19

Why is the condition in this code true?

int main ( )
{

   if (\"\")
      cout << \"hello\"; // executes!

   return 0;
}
         


        
3条回答
  •  隐瞒了意图╮
    2021-01-07 17:46

    Because "" decays to a char const* and all non-null pointers evaluate to true if or when converted to a boolean.

提交回复
热议问题