I accidentally typed =!
instead of !=
that caused a huge bug in a system that went undetected for a while; I have fixed it since but I'm curious as to what =!
does.
I had something like this
void foo(int param)
{
int a = 0;
...
if (a =! param)
{
// never got here even when `a` was not equal to `param`
}
...
}
Can someone explain what the above if
statement is evaluating ?
According to C/C++ operators list there is no operator such as =! . However, there is an operator != (Not equal to, Comparison operators/relational operator)
The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.
The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .
These two operators cannot be compared because they serve different functionalities. The “!= ” operator compares two operands, whereas the “=!” operator inverses the result of boolean values.
This expression:
a =! param
assigns the value !param
to a
. !param
is negated version of param evaluated in boolean context.
Assignment operators return the value of the right hand side, so, if (a = !param)
also executes the if
body, if !param
is true.
It's not a single =!
operator. It's =
and !
, assignment and negation.
It's equivalent to if (a = !param)
, or
a = !param;
if (a) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With