[英]In C, does (x==y==z) behave as I'd expect?
Can I compare three variables like the following, instead of doing if((x==y)&&(y==z)&&(z=x))
? 我可以比较三个变量,如下所示,而不是
if((x==y)&&(y==z)&&(z=x))
? [The if statement should execute if all three variables have the same value. [如果所有三个变量具有相同的值,则应执行if语句。 These are booleans.]
这些是布尔。]
if(debounceATnow == debounceATlast == debounceATlastlast)
{
debounceANew = debounceATnow;
}
else
{
debounceANew = debounceAOld;
}
No, it does not. 不,不是的。
x == y
is converted to int, yields 0
or 1
, and the result is compared to z
. x == y
转换为int,得到0
或1
,并将结果与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)
因此,当且仅当
(x is equal to y and z is 1) or (x is not equal to y and z is 0)
x==y==z
将产生真(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)
否。等式检查从左侧关联,逻辑结果与数字进行比较,因此表达式2 == 2 == 1
解析为(2 == 2) == 1
,这反过来给出1 == 1
结果为1
,这可能不是你想要的。
You can actually type something like this: 你可以输入这样的东西:
int main()
{
const int first = 27,
second = first,
third = second,
fourth = third;
if (!((first & second & third) ^ fourth))
return 1;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.