-2

I always forget which of i++ and ++i return which value. To test this I wrote the fallowing code:

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

The resulting (unexpected and strange) output is:

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

But when I break down the printfs to 4 separate commands, I get the 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
i = 7, ++i = 8

Why does this happens?

SIMEL
  • 8,745
  • 28
  • 84
  • 130
  • 2
    order of evaluation of parameters is unspecifyed – sp2danny Nov 19 '14 at 15:52
  • There are many duplicates of this question. – Jabberwocky Nov 19 '14 at 15:53
  • I don't think the duplicate applies here since the variable is only being modified once. – Bathsheba Nov 19 '14 at 15:58
  • @Bathsheba: `i` is being modified *and read* without an intervening sequence point, which makes the behavior undefined. [C 2011 online draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), 6.5/2: "If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object *or a value computation using the value of the same scalar object*, the behavior is undefined." – John Bode Nov 19 '14 at 16:35
  • @Bathsheba: yes, that's only in the context of the comma acting as an operator; it does not introduce a sequence point when used to separate parameters in a function call. – John Bode Nov 20 '14 at 17:03
  • I've added you comment to my answer and wiki'd it since your edit radically alters the answer. – Bathsheba Nov 20 '14 at 17:07

2 Answers2

7

You have both unspecified and undefined behaviour:

Unspecified behaviour: you don't know the order of evaluation of the parameters in the printf call. (The C standard does not specify this: it's up to the compiler and it's free to choose a way that best matches the machine architecture).

Undefined behaviour: The commas in the function call are not sequencing points. The behaviour is undefined as you're attempting to read and modify the same object without an intervening sequence point.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
-3

Because in you first block i is 6 and after the printf statement it gets incremented because it's postfix!

After that in the second printf statement it's prefix and gets executed before it gets used so 7 + 1 = 8.

In your second code block i is 6 in the first printf statement, because it's postfix! That's why the second output is 7 and the last printf statement has a prefix increment so it gets incremented before it gets used and is 8!

Prefix/ Postfix:

i++  //gets incremented AFTER it gets used (e.g. in a operation)
++i  //gets incremented BEFORE it gets used (e.g. in a operation)

EDIT:

The evaluation order of the arguments of a printf statement is unspecifyed !

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • It doesn't get incremented **after** the first print statement though. Going in `i=6` and the statement prints `i = 7, i++ = 6`. If it were not incremented until after this statement, the output would have been `i = 6, i++ = 6` – Mike Nov 19 '14 at 16:02
  • @Rizier123, the question isn't about what `i++` and `++i` do, but about the unexpected order in which they got executed inside the `printf`. – SIMEL Nov 19 '14 at 16:10
  • @IlyaMelamed yep! Sry first looked every time at the second argument of the printf statement and never saw the first one! edited my answer! (That's clear that this is unspecifyed) – Rizier123 Nov 19 '14 at 16:14