Hello,

main()
{
      int i=4,j=7;
      j = j || i++ && printf("YOU CAN");
      printf("%d %d", i, j);
}

The answer to this question is 4 1 and the reasoning is that the compiler finds j to be true and hence does not need to check the remaining statement
But the precedence of the && operator is greater than the || . So the compiler should compute the operands of the && operator first right ?

Statements are always evaluated from left to right unless parentheses change the order of evaluation. Add parentheses to do what you are thinking (j = j || i++) && printf("YOU CAN");

Edited 6 Years Ago by Ancient Dragon: n/a

Then by your logic

int i= 2+3*5;

would evaluate to 25 .But it evaluates to 17 that is because the multiplication operation is done first, as it has higher precedence.

> Statements are always evaluated from left to right unless parentheses change the order of evaluation.
Only for boolean expressions involving &&, ||, or the ?: operator.

Then by your logic

int i= 2+3*5;

would evaluate to 25 .But it evaluates to 17 that is because the multiplication operation is done first, as it has higher precedence.

No because your original post only involved boolean operators || and &&. As Salem said my statement only applies to the boolean operators.

This article has been dead for over six months. Start a new discussion instead.