-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

Jon V
  • 506
  • 1
  • 3
  • 21
  • 4
    See http://en.wikipedia.org/wiki/Short-circuit_evaluation – Michael Apr 29 '15 at 11:32
  • 1
    Or [this answer of mine](http://stackoverflow.com/a/29141619/2307070) – Thomas Ayoub Apr 29 '15 at 11:33
  • 1
    possible duplicate of [If Statements - What is happening in an "If(..||..)" and "If(...&&...)" construct internally?](http://stackoverflow.com/questions/29141368/if-statements-what-is-happening-in-an-if-and-if-construc) – user-ag Apr 29 '15 at 11:40

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,327
  • 16
  • 31
  • 39