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

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.

commented: good explanation +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.