int x = 10, y = 20, z = 10;
if (x == 10 || y == 20 && (z = 50))
printf("%d", z);
I expected the value of z
in output to be 50
, but it is 10
. If &&
has higher precedence, why is z
not assigned to 50
?
int x = 10, y = 20, z = 10;
if (x == 10 || y == 20 && (z = 50))
printf("%d", z);
I expected the value of z
in output to be 50
, but it is 10
. If &&
has higher precedence, why is z
not assigned to 50
?
If the left operand for ||
is truthy, the RHS is not evaluated. So, the assignment to z
never happens, and the initialised value remains.
This is called Short-circuit evaluation