-1

So, I understand that i++ increments post the condition is fulfilled, while ++i increments before the condition is fulfilled. That takes me to my question:

#include<stdio.h>
int main()
{
    int i=0;
    while(++i<10)
        printf("%d\n",i);
    return 0;
}

Now here we need the initialization of i from 0 as when it goes in the while loop, it will get incremented first, and thus it will be like while(1<10), and so it will print numbers from one to ten.

Second code:

#include<stdio.h>
int main()
{
    int i=0;
    while(i++<10)
        printf("%d\n",i);
    return 0;
}

Now since i++ increments after the value has been used, so why do we initialize i from i=0, as if it gets incremented after the comparision of value of i takes place, then why isn't 0 getting printed as well because the first loop should go like while(0<10), and not like while(1<10)? So, why is zero not getting printed?

Thank you for your time.

  • But it's written that i++ increments after the comparison, so, then what's the difference between ++i and i++? – Tanmay Sharma May 16 '21 at 12:21
  • 1
    The better question to ask is "why does one loop print 10, and the other does not?" – William Pursell May 16 '21 at 12:22
  • 1
    Note that "after the comparison" occurs before the printf. – William Pursell May 16 '21 at 12:25
  • With i++ you get numbers from 1 to 10, while with ++i you get numbers from 1 to 9. If I put a breakpoint and go step by step then you can see that after the comparison the number i is incremented, so in the print it will start always with 1. – sheldor May 16 '21 at 12:25
  • `"Now since i++ increments after the value has been used"` -- This statement is not quite correct. The expression `i++` will evaluate to the value that `i` had before incrementing. But this does not specify whether the actual incrementation takes place before or after evaluating the `if` condition. See this question for further information: [Why are these constructs using pre and post-increment undefined behavior?](https://stackoverflow.com/q/949433/12149471) – Andreas Wenzel May 16 '21 at 12:46
  • @sheldor: Your claim that the debugger will show that the compiler will always increment `i` after the comparison, is not necessarily correct. First of all, the compiler is always free to reorder instructions under the [as-if rule](https://stackoverflow.com/questions/15718262/what-exactly-is-the-as-if-rule). For example, it may make a copy of `i`, then increment `i`, and then compare the copy. Also, even without the as-if rule, as I have pointed out in my previous comment, the incrementation operation is unsequenced with respect to the comparison. – Andreas Wenzel May 16 '21 at 12:55

6 Answers6

3

Zero isn't printed because i is incremented right after the comparison, like this:

while(i<10) {
    i += 1;
    printf("%d\n",i);
}

The first sentence of your question is almost the answer:

i++ increments post the condition is fulfilled

It's incremented after the condition is checked, so i++ < 10 will increment i regardless of whether the condition ends up true or false.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
3

The difference of pre- and post- increment applies only to the value that the expression evaluates to.

After the evaluation of i++<10 is completed, the incrementation is also completed, and i has the value 1 in the first iteration.

printf("%d\n",i); is executed after the evaluation of i++<10, so i has the value 1 here in the first iteration.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
3

Yes, the first condition will evaluate to 0 < 10. You can verify that by changing it to while (i++ < 1) and see that the loop still runs (once), so clearly it's using 0 for the condition (1 < 1 would be false of course).

So why does it print 1? Because it doesn't print whichever value was used for the condition. It prints the current value of i. And the current value of i is 1 at that point because i was incremented right after it was used in the condition.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
1

The operation of ++ is not done before or after any “condition” it is in. It must be completed sometime in or around the full expression it is in. The answers that say i is updated “before” or “after” the ++i or i++ is evaluated are incorrect.

Prefix ++i does two separate things that may happen in any order:

  • It evaluates to the value of i after one is added.
  • It adds one to the stored value of i.

Postfix i++ does two separate things that may happen in any order:

  • It evaluates to the value of i before one is added.
  • It adds one to the stored value of i.

That change to the stored value of i is called a side effect. It is disconnected from the main evaluation of the expression. It can be performed before, during, or after the evaluation of i, but the evaluation still returns the pre- or post-increment value, as described above.

A full expression is one that is not contained inside another expression. So, in while (++i < 10), ++i < 10 is a full expression. The side effect can occur before any part of that is evaluated, after the value of i plus one is calculated, or after the < is evaluated. It can also occur in parts (such as updating the bytes of i one by one) during the evaluations. However, the side effect must occur after any previous full expression and before any later full expression. That is because the C standard says there is a sequence point between any two full expressions. (And there are some other rules about order of execution.)

Even if the stored value of i is updated before i++ or after ++i, the expression must still produce the value of i before or after the increment, respectively. For example, for i++, the compiler can fetch i, add one, store i, but then use the pre-add value in the expression.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

The result of i++ is the value of i. As a side effect i is incremented.

The result of ++i is the value of i + 1. As a side effect i is incremented.

The statement

x = i++;

is logically equivalent to

tmp = i;
x = tmp;
i = i + 1;

with the caveat that the assignments to x and i can happen in any order, or even simultaneously (interleaved or in parallel).

Similarly, the statement

x = ++i;

is logically equivalent to

tmp = i + 1;
x = tmp;
i = i + 1;

with the same caveat as above. Again, these are logical equivalents, not what the compiler actually generates - depending on the compiler and the code involved there may not be a temporary.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

In the second code, the increment will take place after the condition but before the printing statement.

Thus, the incremented value of i is printed.

This is your answer in simple language.

Utkarsh Sahu
  • 409
  • 3
  • 16