in Programming in C
890 views
7 votes
7 votes

What will be the outout of the following code?

#include <stdio.h>
int main()
{
    int a = 1, b = 2;
    int c = a++ || b++;
    printf("%d %d %d", a, b, c);
}
  1. 1 2 1
  2. 2 3 1
  3. 2 2 1
  4. 2 2 0
in Programming in C
by
890 views

2 Answers

12 votes
12 votes
Best answer
int a = 1, b = 2;
int c = a++ || b++;

2nd statement will run a++ and then there is logical OR operator so it will halt. (Short Circuit Rule for logical operators in C).

c = 1,
a = 2,
b = 2 (unaffected)
selected by

3 Comments

what are the short circuit rule in c plz explain
0
0
for logical AND, if the first operand is false (0), second is not evaluated. Similarly for logical OR, if the first operand is 1, the second one is not evaluated.
9
9
a++ will be evaluated first or logical OR ..a++ is post increment so is it ???

int c = a || b;

a++ ; b++
0
0
1 vote
1 vote

    int c = a++ || b++;

The logical OR would stop as soon as it finds any positive value ($\equiv$ 1). It stops right at a.

So, c = a

=> c = 1

 

Now, a++

=> a = 2

 

And b is never touched.

So, 2,2,1.

Option C

Answer:

Related questions