Above line of code exhibits undefined behavior. For an LOC(line of code) with undefined behavior, you can not question why, how etc. (Output differs with compiler, differs with time etc)
LOC of interest exhibits undefined behavior because variable i is being updated twice before next sequence point is reached.
This faq is worth a read for you.
Between the previous and next sequence
point an object shall have its stored
value modified at most once by the
evaluation of an expression.
Furthermore, the prior value shall be
read only to determine the value to be
- 6.5.16 Assignment operators, §4:
The order of evaluation of the
operands is unspecified. If an attempt
is made to modify the result of an
assignment operator or to access it
after the next sequence point, the
behavior is undefined.
Any LOC in C language can show one of 5 behaviors given below:
- Well defined: For ex value of i
after i = 2 + 3 * 5;
- Implementation dependent: For ex value of sz in size_t sz = sizeof (int);
- Unspecified: For ex: Order of evaluation in, add(print(“hi”), print(“Hello”));
- Undefined: For ex: value of i afer i = 0; i = i++; [it can be 0 or 1 or 100 or -500]
- Locale specific: For example what would be printed when you do printf("\\");
If you still find something unclear, please ask.