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
200_success
  • 7,286
  • 1
  • 43
  • 74
Stopdrake
  • 37
  • 1
  • 5
  • 2
    Undefined behavior, on operator `z`. – mazhar islam Jun 20 '15 at 05:53
  • 2
    check this [question](http://stackoverflow.com/questions/29998697/difference-between-i-and-i); – Himanshu Jun 20 '15 at 05:55
  • 2
    @rakeb.void Not in this case. `||` (and `&&`) is a sequence point, and its short-circuiting behavior avoids evaluation of `++z`. – jamesdlin Jun 20 '15 at 06:09
  • 1
    Why write code that is difficult to read? Job security problem – Ed Heal Jun 20 '15 at 06:17
  • Possible duplicate of [Why does "++x || ++y && ++z" calculate "++x" first, even though operator "&&" has higher precedence than "||"](https://stackoverflow.com/questions/3700352/why-does-x-y-z-calculate-x-first-even-though-operator-ha) – phuclv Aug 18 '18 at 11:20

4 Answers4

5

++ has higher priority than ||, so the whole RHS of the assignment boils down to an increment of x and an evaluation to a truth value (1).

z = ++x         ||  ++y&&++z;
    truthy (1)     never executed

This is because ++x evaluates to true and the second branch is not executed. ++x is 2 which, in a boolean context, evaluates to true or 1. z takes the value of 1, giving you the observed final state.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • One of `z = ++x||0;` or `z = !!++x;` or `z = (_Bool)++x;` or `z = ++x ? 1 : 0;` would be more appropriate. `z = ++x;` would be `z = 2;` –  Jun 20 '15 at 05:58
5
x=y=z=1
z=++x||++y&&++z

is equivalent to

x=y=z=1
z=((++x)||((++y)&&(++z)));

Since ++x returns 2, which is nonzero, the ++y && ++z branch is never executed, and thus the code is equivalent to:

x=y=z=1;
z=(++x)||(anything here);
4

z=++x||++y&&++z;

NOTE: ++ has higher priority than ||

Now after this line is executed the value of x is incremented and x=2 now ++y&&++z are never executed as the first condition is true and hence you are getting the value as x=2 y=1 z=1

Vinay Shukla
  • 1,818
  • 13
  • 41
3

The and && and the or || operation is executed from left to right and moreover, in C 0 means false and any non-zero value means true. You write

x=y=z=1;
z= ++x || ++y && ++z;

As, x = 1, so the statement ++x is true. Hence the further condition ++y && ++z not executed.

So the output became:

x=2 // x incremented by 1
y=1 // as it is
z=1 // assigned to true (default true = 1)

Now try this,

z= ++y && ++z || ++x ;

You will get

x=1 // as it is because ++y && ++z are both true 
y=2 // y incremented by 1
z=1 // although z incremented by 1 but assigned to true (default true = 1)

And finally try this:

int x = 1;
int y = 0;
int z = 1;

z= y && ++z || ++x;

The output will be:

So the output became:

x=2 
y=0 
z=0 

Because, now the statement for z is look like this:

z = false (as y =0) && not executed || true
z = false || true
z = true

So, y remains same, x incremented and became 2 and finally z assigned to true.

mazhar islam
  • 5,561
  • 3
  • 20
  • 41