Why c is not incremented in the output?
Asked Answered
G

5

5

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 ?

Gutsy answered 22/2, 2012 at 16:22 Comment(4)
d = ( (-2 && 3) || (++c) ). It's short circuited before it gets to c.Poky
What are you actually trying to do? Or are you just simply playing with some code?Candida
simply solving c puzzlesGutsy
Thank u all for ur help . Now able to understand the situation better. :)Gutsy
I
10

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.)

Isochronize answered 22/2, 2012 at 16:24 Comment(1)
Maybe he does not understand how -2 and 3 evaluate to true.Poky
S
9

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.

Souter answered 22/2, 2012 at 16:24 Comment(0)
E
2

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.

Eleaseeleatic answered 22/2, 2012 at 16:27 Comment(0)
H
2

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.

Herbalist answered 22/2, 2012 at 16:28 Comment(0)
W
2
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...

Willpower answered 22/2, 2012 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.