The Logical NOT Operator: !
The !
operator negates, or reverses the truth value of, the expression that follows it. That is, if expression
is true
, then !expression
is false
—and vice versa. More precisely, if expression
is true
, or nonzero, then !expression
is false
. Incidentally, many people call the exclamation point bang, making !x
“bang-ex” and !!x
“bang-bang-ex.”
Usually you can more clearly express a relationship without using the !
operator:
if (!(x > 5)) // if (x <= 5) is clearer
But the !
operator can be useful with functions that return true
/false
values or values that can be interpreted that way. For example, strcmp(s1,s2)
returns a nonzero (true
) value if the two C-style strings s1
and s2
are different from each other ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.