Post and Pre Increment in C
c
Solution
It's undefined behavior. There's no sequence points between the increments of `i`.
Any result is a correct result (including your hard drive being formatted).
Problem
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) There is a code following below and i ma facing a very serious problem in understanding the logic for the code. ``` #include <stdio.h> #include <stdlib.h> int main(void ) { int i = 1 ; printf("\n%d %d %d %d\n",++i,i++,i++,++i) ; return 0 ; } ``` I am using gcc compiler under the linux distro named Mandriva. In the above mentioned i have used pre and post increment with a variable in the printf statement. The output that i am supposed to get is 2 2 3 5, but i am getting a different output. Please help me in this code. I am feeling much difficult in this code.
Related problems
- Why are these constructs using pre and post-increment undefined behavior?
- What are sequence points, and how do they relate to undefined behavior?
- Undefined behavior and sequence points reloaded
- What is the relation between operator precedence and order of evaluation?
- Sequence points and partial order
- Unsequenced value computations (a.k.a sequence points)
- Where do sequence points come from?
- Sequence points and order of evaluation