I was studying operator precedence and I am not able to understand how the value of x
became 2
and that of y
and z
is 1
x=y=z=1;
z=++x||++y&&++z;
This evaluates to
x=2 y=1 z=1
I was studying operator precedence and I am not able to understand how the value of x
became 2
and that of y
and z
is 1
x=y=z=1;
z=++x||++y&&++z;
This evaluates to
x=2 y=1 z=1
z=++x||++y&&++z;
NOTE:
++
has higher priority than||
Now after this line is executed the value of
x
is incremented andx=2
now++y&&++z
are never executed as the first condition is true and hence you are getting the value asx=2 y=1 z=1
is equivalent to
Since
++x
returns2
, which is nonzero, the++y && ++z
branch is never executed, and thus the code is equivalent to:The and
&&
and the or||
operation is executed from left to right and moreover, in C0
meansfalse
and any non-zero value meanstrue
. You writeAs,
x = 1
, so the statement++x
istrue
. Hence the further condition++y && ++z
not executed.So the output became:
Now try this,
You will get
And finally try this:
The output will be:
So the output became:
Because, now the statement for z is look like this:
So,
y
remains same, x incremented and became2
and finallyz
assigned totrue
.++
has higher priority than||
, so the whole RHS of the assignment boils down to an increment ofx
and an evaluation to a truth value (1).This is because
++x
evaluates to true and the second branch is not executed.++x
is2
which, in a boolean context, evaluates to true or1
.z
takes the value of1
, giving you the observed final state.