Post / pre increments in 'printf'

Possible duplicates:
Output of several messages and preliminary increments in one report
Step 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); 

gives way

2 2

Can someone explain the second behavior to me?

0
source share
3 answers

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. :-)

+11
source

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.

+3
source

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.

+1
source

All Articles