The unary !
logical negation operator, applied to any scalar, yields the int
value 0
if its operand is non-zero, 1
if the operand is equal to zero. Quoting the standard:
The expression !E
is equivalent to (0==E)
.
Applying !
twice to the same scalar value yields a result that's false if the value is false, true if the value is true -- but the result is normalized to 0
or 1
, respectively.
In most cases, this isn't necessary, since any scalar value can be used directly as a condition. But in some cases you actually need a 0
or 1
value.
In C99 or later, casting the expression to _Bool
(or to bool
if you have #include <stdbool.h>
behaves similarly and might be considered clearer. But (a) the result is of type _Bool
rather than int
, and (b) if you're using a pre-C99 compiler that doesn't support _Bool
and you've defined your own bool
type, it won't behave the same way as C99's _Bool
.