Output the next program in C

What should be the result of this C program?

#include<stdio.h>
int main(){
  int x,y,z;
  x=y=z=1;
  z = ++x || ++y && ++z;
  printf("x=%d y=%d z=%d\n",x,y,z);
  return 0;
}

This result: x = 2 y = 1 z = 1
I understand the output for x, but I do not see how the values ​​of y and z do not increase.

+3
source share
2 answers

This is the result of a short circuit assessment .

The expression ++xevaluates to 2, and the compiler knows that it 2 || anythingalways evaluates 1("true") no matter what anything. Therefore, he does not go to the assessment anything, and the values yand zdo not change.

If you try using

x=-1;
y=z=1;

, y z , OR .

: asaerl , .

, , , . , OR, ,

++x || (++y && ++z)

(++x || ++y) && ++z

OR ++x ++y && ++z. , , , "" - - . .

, , || && , , , rhs, , lhs .

+12

C , 0, , || .

, , , . . A || B - , A , , B . A , B , B , true B false, false.

++ x (i.e 2), - , 0, C. , / .

+1

All Articles