What is "!!" in C? [duplicate]
!
is negation. So !!
is negation of negation. What is important is the fact that the result will be an int
.
-
!!x
ifx == 0
is!!0
, that is!1
, that is0
. -
!!x
ifx != 0
is!!(!0)
, that is!!1
, that is!0
, that is1
.
!!
is used commonly if you want to convert any non-zero value to 1 while being certain that 0 remains a 0.
And indeed, !!NULL == NULL
, since !!NULL == !!0
and !!0 == !1
and finally !1 == 0
.
Consequently, in the short piece of code you cited the array subscript will be either 0
if the value of the expression in parenthesis is NULL
, and 1
otherwise.
It is commonly (ab)used to convert any value into the int
s 0 or 1 by repeated application of the boolean not operator, !
.
For instance: !56
is 0, since 56 is "true" when viewed as a boolean. This means that !!56
is 1, since !0
is 1.