Possible duplicates:Output of several messages and preliminary increments in one reportStep increment and pre-increment in the 'for' loop
The following code snippet
int i=0; printf("%d %d",i++,i++);
gives way
ten
I can understand it, but the following
int i=0; printf("%d %d",++i,++i);
2 2
Can someone explain the second behavior to me?
Both printfs call undefined -behavior. See the following: Undefined behavior and sequence points
Quoted from this link:
In short, undefined behavior can all happen to demons flying out of the nose of a girlfriend to become pregnant.
For beginners: Never try to change the values โโof your variables in the list of function arguments twice . For more information, click here to find out what this means. :-)
They are both undefined. Changing the variable i more than once is undefined. Also C ++ or C? You have to decide how the behavior of pre-increment, I believe, is different from them.
i
You got what is called "undefined behavior" because you change the same variable more than once between points in a sequence. Another compiler may give you different results.