EmbLogic's Blog

facing associativity issue in last two statments of the followin programme…pls explain it

{
int x=1,y=1,z=1;
x+=y+=z;
printf(“%d\n”,x<y?y:x);
printf(“%d\n”,x<y?x++:y++);
printf(“%d\n”,x);printf(“%d\n”,y);
printf(“%d\n”,z+=x<y?x++:y++);
printf(“%d\n”,y);printf(“%d\n”,z);
x=3,y=z=4;
printf(“%d\n”,z>=y>=x?1:0);
printf(“%d\n”,z>=y&&y>=x)

2 Responses to facing associativity issue in last two statments of the followin programme…pls explain it

  1. step (1) here we have three operator
    step (2) >= will be executed first than conditional operator because of more precedence.
    step (3) we know if there are more than one operator of same type then we see associativity and for >= opreator we move left to right side.
    hence….here

    ist statement z>=y>=x?1:0 will be seen as
    z>=y true so 1 now is becomes
    1>=x?1:0
    1>=x false 0 now it becomes
    0?1:0
    this is conditional operator
    result is 0
    similerly we see next statement .

  2. we execute first
    uniary operators ,
    binary operator
    , logical operators
    shifting operators
    logical
    then assignment operators

Leave a Reply to ashish.dahiya Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>