Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why c is not incremented in the output?

I was working on the Basics of C and was trying to solve the problem below could any one explain why the output of variable c is different?

What is the output of the following program?

int main()
{
   int a = -3, b = 2, c= 0, d;
   d = ++a && ++b || ++c;
   printf ("a = %d,  b = %d, c = %d, d = %d", a, b, c, d);
} 

Ans: -2, 3, 0, 1

Why c is not incremented in the output ?

like image 996
Nisha Avatar asked Feb 22 '12 16:02

Nisha


5 Answers

The variable c is not incremented because the RHS (right-hand side) of an || is not executed unless the LHS evaluates to false, and the LHS evaluates to true. The C || and && operators are 'short-circuit' operators; they do not evaluate the second operand unless the first is insufficient to determine the overall truth of the expression.

The && binds tighter than the ||, so the operation can be parenthesized as:

d = (++a && ++b) || ++c;

The value of ++a is -2, which evaluates to true (because any value that is not 0 evaluates to true); the value of ++b is 3, which evaluates to true; so the value of the && term is true. Because true || false and true || true both evaluate to true, there is no need to evaluate the RHS to know the overall result. (The analogous rule for && is that if the first term evaluates to false, there is no need to evaluate the second because the overall expression must be false. If you had a = -1; before the test, then b would not be incremented, because ++a would be zero or false, so the RHS of the && is unevaluated. Of course, then c would be incremented because the LHS of the || would be false, and the RHS would have to be evaluated to determine the overall result.)

like image 137
Jonathan Leffler Avatar answered Oct 22 '22 06:10

Jonathan Leffler


Because ++a && ++b evaluates to true.

It's called short-circuiting. Expressions inside conditions are evaluated from left-to-right. If your case, if the first condition in the OR clause is evaluated to true, there's no point for the second one to also be evaluated, since the whole expression is known to be true already.

like image 35
Luchian Grigore Avatar answered Oct 22 '22 04:10

Luchian Grigore


In C, the boolean logic operators && and || are short-circuiting. This means that they only evaluate their right side, if evaluating the left side is not enough to know the answer.

For your code, this has the effect of never evaluating ++c, since the left-hand side is not zero and thus the boolean or's result will be true, and there's no need to do more work.

like image 2
unwind Avatar answered Oct 22 '22 04:10

unwind


It is lazy boolean expressions evaluation. The execution is:

++a gives -2

++b gives 3

-2 && 3 gives 1

Okay! No need to check result of ||. So ++c is not evaluated.

The rule is: expression part X is not evaluated in cases: (0 && X), (1 || X). Here 1 is "not 0" of course.

like image 2
AlexZam Avatar answered Oct 22 '22 06:10

AlexZam


d= ++a && ++b || ++c
d= ++(-2) && ++b || ++c
d= -1 && ++b || ++c
d= true && ++b || ++c
d= true && ++2 || ++c
d= true && true || ++c
d= true || ++c
d= 1

That's roughly how it works behind the scene...

like image 2
jasonkim Avatar answered Oct 22 '22 04:10

jasonkim