c - Strange behavior of printf and i++ -


this question has answer here:

i forget of i++ , ++i return value. test wrote fallowing code:

int i; = 6; printf ("i = %d, i++ = %d\n",i, i++); printf ("i = %d, ++i = %d\n",i, ++i); 

the resulting (unexpected , strange) output is:

i = 7, i++ = 6 = 8, ++i = 8 

but when break down printfs 4 separate commands, expected result:

printf ("i = %d, ",i); printf ("i++ = %d\n",i++); printf ("i = %d, ",i); printf ("++i = %d\n",++i); 

gives:

i = 6, i++ = 6 = 7, ++i = 8 

why happens?

you have both unspecified , undefined behaviour:

unspecified behaviour: don't know order of evaluation of parameters in printf call. (the c standard not specify this: it's compiler , it's free choose way best matches machine architecture).

undefined behaviour: commas in function call not sequencing points. behaviour undefined you're attempting read , modify same object without intervening sequence point.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -