0
int main(int argc, char* argv[])
{
        int a=3,b=2,c;
        a=a==b==0;
        printf("%d\n",a);

        return 1;
}

I cannot understand why this code gives 1 as the answer.

According to me , this should be the order of evaluation
b==0, which gives false & the expression becomes a=a==0
this should again give false & the expression should become
a=0
thereby the value of a should be 0

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by IsharaComix
1

According to me , this should be the order of evaluation
b==0, which gives false & the expression becomes a=a==0
this should again give false & the expression should become
a=0
thereby the value of a should be 0

Actually, what's happening is a = (a==b==0) which is equivalent to a = ( ( a==b ) == 0 ) a == b is 0, and 0 == 0 is true, so a is one. The assignment associativity is right to left, but the conditional associativity is left to right.

Edited by IsharaComix: n/a

Votes + Comments
good explanation
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.