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

前端 未结 3 412
甜味超标
甜味超标 2021-01-11 16:08

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 vari

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-11 16:17

    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)
    

提交回复
热议问题