1
#include <stdio.h> 
main()
{
    int x = 1, y = 0, z = 5; 
    int a = x && y || z++;  
    printf("%d", z); 
}

This code gives me output : 6

#include <stdio.h> 
main()
{
    int x = 1, y = 0, z = 5; 
    int a = x && y && z++;  
    printf("%d", z); 
}

This code gives me output : 5

Why the second program gives ouput as 5 even though z was incremented same like in first program?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Difference between “||” and “|” It’s the same as above, in the case of “||” only one statement is executed and if it returns “true” then the other statement will not be executed. But if the first is false then the other will be checked for the value “true”. The reason for this is the way the “or” operator works. The “Or” operator depends on only one true, in other words if any of the expressions are true then the result will be true. – RïshïKêsh Kümar Apr 27 '17 at 01:06
  • https://www.codeproject.com/Articles/662248/Difference-between-and-and – RïshïKêsh Kümar Apr 27 '17 at 01:08
  • duplicate of [Why does `++x || ++y && ++z` calculate `++x` first, even though operator `&&` has higher precedence than `||`](https://stackoverflow.com/q/3700352/995714) – phuclv Aug 18 '18 at 11:25

3 Answers3

3

Evaluation of x && y && z++ stops after y because y==0. z++ is not executed.

mvds
  • 45,755
  • 8
  • 102
  • 111
3

The && operator (as well as the || operator) is a short-circuit operator. That means that if the left operand evaluates false, the result is false regardless of the right operand, so the right operand is not evaluated.

From section 6.5.13 of the C standard:

4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares equal to 0, the second operand is not evaluated.

So in the case of this expression:

x && y && z++;

x && y is evaluated first. Since x is 1, y is evaluated and found to be 0, so the result of the first && is 0. Now we have:

0 && z++

Since the left operand is 0, the right operand is not evaluated. This means that z is not incremented.

This is in contrast to expression in the first piece of code:

x && y || z++

As before, x && y evaluates to 0, leaving us with:

0 || z++

Since the left operand is 0, the right operand needs to be evaluated. This results in z getting incremented.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

The initializer in this declaration

int a = x && y || z++;

is equivalent to

int a = ( x && y ) || ( z++ );

According to the C Standard relative to the logical OR operator (6.5.14 Logical OR operator)

4 ... If the first operand compares unequal to 0, the second operand is not evaluated.

However the first operand that is ( x && y ) compares equal to 0 (due to the variable y is equal to 0). So the second operand ( z++ ) is evaluated.

The initializer in this declaration

int a = x && y && z++;

is equivalent to

int a = ( x && y ) && ( z++ );

According to the C Standard relative to the logical AND operator (6.5.13 Logical AND operator)

4 ...If the first operand compares equal to 0, the second operand is not evaluated

So as the first operand ( x && y ) compares equal to 0 then the second operand ( z++ ) is not evaluated.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335