is there a pragmatic reason to use “if (0 == p) ” instead of “if (!p)”?

后端 未结 12 750
难免孤独
难免孤独 2021-02-05 06:18

I\'m inclined to write if statements by using logical negation operator:

if (!p)
    some_code();

Some people around me tend to use explicit co

12条回答
  •  粉色の甜心
    2021-02-05 07:12

    Some claim that the pragmatic benefit is that programmers will find it easier to understand if you explicitly compare against NULL, FALSE, 0, etc., whereas the logical operator may be confusing to people who don't understand how implicit conversions and booleans work in C/C++.

    (Disclaimer: I don't share this view myself. if (p) ... and if (!p)... are the idiomatic ways to express this in C and C++, and programmers who have trouble understanding them have no business touching C or C++ code. Heath Hunnicutt's comment is dead on.)

提交回复
热议问题