Why is output of this code not “true”?

  • A+

According to my limited knowledge logical operators have left-right order of evaluation. By that rule it should evaluate x && y which is 1 and then it should evaluate == 1 in the if statement. But that is what not happening. Could anyone help me with this.

int main() {     int x = 1, y = 2;     if (x && y == 1)         printf("true/n");     else         printf("false/n"); } 


The order of operations is different than what you think.

Your expression is equivalent to

x && (y==1) 

which is false in your case.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: