Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C, does (x==y==z) behave as I'd expect?

Tags:

c

equality

Can I compare three variables like the following, instead of doing if((x==y)&&(y==z)&&(z=x))? [The if statement should execute if all three variables have the same value. These are booleans.]

if(debounceATnow == debounceATlast == debounceATlastlast)
{
 debounceANew = debounceATnow;
}
else
{
 debounceANew = debounceAOld;
}
like image 214
Isaac Avatar asked Dec 07 '10 15:12

Isaac


1 Answers

No, it does not.

x == y is converted to int, yields 0 or 1, and the result is compared to z. So x==y==z will yield true if and only if (x is equal to y and z is 1) or (x is not equal to y and z is 0)

What you want to do is

if(x == y && x == z)
like image 132
Armen Tsirunyan Avatar answered Sep 22 '22 19:09

Armen Tsirunyan