660 views

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

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)

what are the short circuit rule in c plz explain
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.
a++ will be evaluated first or logical OR ..a++ is post increment so is it ???

int c = a || b;

a++ ; b++

    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