The following code snippet illustrates this behaviour:
| Code | Result |
|---|---|
#include |
-2 is TRUE -1 is TRUE 0 is FALSE 1 is TRUE 2 is TRUE |
There are cases in which relying on this convention creates quite confusing code. A well known example is the memcmp function, which return 0 if the compared memory areas are equivalent. This leads to code like
if (!memcmp(p1, p2, 4)) { ...
which is confusing to read since the ! operator is normally
read as "not".
It is recommended to avoid this and use a more clear
way to express the same thing:
if (memcmp(p1, p2, 4) == 0) { ...