0

In the code below, I was expecting answer 'Yes'. But the answer is no. That means i is getting incremented after && operation. I was expecting that i gets incremented once entire expression inside if() is evaluated. So what are the rules associated with post increment?

int main()
{
    int i = 1;
    if (i++ && (i == 1))
        printf("Yes\n");
    else
        printf("No\n");
}
Rajesh
  • 1,085
  • 1
  • 12
  • 25
  • 6
    For `&&`, the left side is fully evaluated first, which includes incrementing `i`. It's not `++` you should be asking about, but rather `&&`. – Cornstalks Aug 10 '17 at 02:05
  • Is this the case for all logical operators or only few of them? – Rajesh Aug 10 '17 at 02:07
  • 3
    It's the case for `&&` and `||`, because of their [short-circuiting behavior](https://stackoverflow.com/questions/628526/is-short-circuiting-logical-operators-mandated-and-evaluation-order). Both of those operators will always fully evaluate the left-hand argument first. Depending on the result of that evaluation, the may (or may not) continue and evaluate the right-hand argument. – Cornstalks Aug 10 '17 at 02:10
  • 2
    This is also known as a `sequence point`. If a sequence point is present between the subexpressions which it is in this case, then both value computation and side effects of the first subexpression, i.e. `i++` are sequenced-before every value computation and side effect of the second subexpression. – bruceg Aug 10 '17 at 02:12
  • Here's everything you'll ever need to know about how ++ works: don't. Don't ever use it, and it will never bite you. Saving a couple of keystrokes over `i += 1` isn't worth the hassle. – Lee Daniel Crocker Aug 10 '17 at 02:19
  • You will also want to familiarize yourself with [**C11-Standard (Committee Draft - n1570) §6.5.2.2**](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), particularly Nos. 10 & 12 regarding the sequencing of functions calls and the indeterminately ordered arguments. – David C. Rankin Aug 10 '17 at 03:54

2 Answers2

3

The logical AND operator && has a sequence point between the evaluation of the left operand and the right operand. That means the left operand is evaluated first, along with any side effect (in this case the increment).

In this expression, i starts with a value of 1. So the expression i++ has a value of 1, and the value of i is incremented.

As the left side of the && operator this value evaluates to true, so the right side is then evaluated. If the value was 0 i.e. false the right side would not be evaluated.

At this point the value of i is 2, so i == 1 evaluates to false, making the result of the && operator false. This results in the else clause being executed.

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

it compiles from left to right, up to down, so it's logical that you're having a 'No', your if statement contains i++ which will cause i to be equal to 2 and then returns true, then it contains i==1 which will return false since i is no longer equal to 1, so your if statement is returning false which will make it execute what's on the else, means 'No'.

A simple explanation about logical operators:

&&: evaluated from left to right, and all the evaluated conditions must return 'true' so it can be true.

||: evaluated from left to right as well, except that it ends the evaluation with true, once finding one true condition.

Community
  • 1
  • 1