Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "=!" operator do? [closed]

Tags:

c++

c

operators

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 ?

like image 765
Professor Chaos Avatar asked Jan 09 '13 03:01

Professor Chaos


People also ask

What does =! Mean in C?

According to C/C++ operators list there is no operator such as =! . However, there is an operator != (Not equal to, Comparison operators/relational operator)

What does the || operator do?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What does != Mean in C++?

The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

Is != The same as =!?

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.


2 Answers

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.

like image 145
perreal Avatar answered Jan 05 '23 15:01

perreal


It's not a single =! operator. It's = and !, assignment and negation.

It's equivalent to if (a = !param), or

a = !param;

if (a) {

}
like image 23
meagar Avatar answered Jan 05 '23 14:01

meagar