Why is the statement using relation operators evaluate to 1 in the following code?

北城余情 提交于 2019-12-04 07:14:35

问题


Shouldn't the value of i be 0? Since x

    #include<stdio.h>

    int main(void)
    {
         int x = 10,y=20,z=5,i;
         i=x<y<z;
         printf("%d",i);
         return 0;
    }

回答1:


In your code, due to the LTR associativity of the relational operators,

 i=x<y<z;

evaluates to

i=(x<y)<z;

which is

i=(10<20)<z;

which is

i= 1 < 5;

which is TRUE (1). That 1 gets stored in i. That's it.



来源:https://stackoverflow.com/questions/34481683/why-is-the-statement-using-relation-operators-evaluate-to-1-in-the-following-cod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!