-4
#include<stdio.h>
int main()
{
   int i=0, k=0, m;
   m = ++i || ++k;
   printf("%d, %d, %d\n", i, k, m);
   return 0;
 }

returns

1,0,1

Why is k = 0 and not 1? what is the effect of the ||-operator on the ++k? Thanks!

example: https://ideone.com/Fjsbii

Jonathan V
  • 496
  • 1
  • 3
  • 19

1 Answers1

2

In || OR , if first condition is true, it will not check second condition.(it will skip 2nd condition).

As

m = ++i || ++k;

in this condition after ++i, value of i will become 1, as first condition is true, so it will skip second condition. so the operation ++k will not be performed.
And hence k will remain 0.

Same as if you are using && , and first condition is false it will skip second condition. and result will be 0 (false).

Himanshu
  • 4,269
  • 16
  • 29
  • 37