-2

I don't know if this is compiler specific but when I tried running the two expressions in DevC++

When i=c=b=0; i=i++ + ++c gives 2 whereas i=++i + c++ gives 1

But b=i++ + ++c and b=++i + ++c produces the result 1 for both expressions.

I do know that incrementing a variable twice in the same expression results in an undefined value according to the C standard specification but I'm curious how the compiler produces these output. Could someone please explain how and why?

ann
  • 576
  • 1
  • 10
  • 19
Monty Swanson
  • 695
  • 16
  • 41
  • 5
    Undefined behavior. [Demons may fly out of your nose](http://www.urbandictionary.com/define.php?term=nasal%20demons). Actually, this question would be interesting if you didn't assign the result back to `i`, which causes the undefined behavior. – Fred Larson Jun 26 '11 at 03:54
  • @Fred Larson in the 2nd example he doesnt which is valid but the first one is undefined as you mentioned – Jesus Ramos Jun 26 '11 at 03:57
  • @Jesus: I see assignment back to `i` in both, so both are undefined behavior. No sequence point between the assignment and the increment. – Fred Larson Jun 26 '11 at 03:59
  • Theres one where he says b=i++ + ++c, this one is valid as is the one next to it. – Jesus Ramos Jun 26 '11 at 04:01
  • @Jesus: Oh, I see what you mean. Yes, there's no undefined behavior there. And that result makes sense. In one it's `0 + 1`, in the other it's `1 + 0`. – Fred Larson Jun 26 '11 at 04:03
  • @fred: Is it not defined that RHS is fully evaluated before assigning it to LHS? – grok12 Jun 26 '11 at 04:15
  • Point of interest: Although these example are undefined in c++ (probably because they figured no one would be crazy enough to actually use something like this), the behavior is well defined in Java. They don't trust us not to make bad choices :) – jpm Jun 26 '11 at 04:18
  • 2
    @grok12: No, the postincrement can be performed anytime after the variable's value is used in the expression. It's not defined whether it's performed before or after the assignment. – Fred Larson Jun 26 '11 at 04:23
  • @Fred: +1 for that. I wouldn't have guessed it but I'm glad I know it now. – grok12 Jun 26 '11 at 04:30
  • so this is marked as duplicate after 2 years?? nice – Monty Swanson Feb 09 '14 at 10:28

4 Answers4

3

i++ + ++c, the c is incremented (to 1), then 0 + 1 is stored in i, and finally i is incremented, giving 2.

++i + c++, the i is incremented (to 1), then 1 + 0 is stored in i, then c is incremented.

That's how I would understand what the compiler did, but as everyone else is saying, don't count on this behavior elsewhere.

ann
  • 576
  • 1
  • 10
  • 19
Fred
  • 8,582
  • 1
  • 21
  • 27
1

Are you sure b = ++i + ++c = 1? or was it b = ++i + c++? Here is my explanation of your question.

    i = i++ + ++c
    (i = 0 + 1)++
    i = 2
    c = 1

    i = ++i + c++
    (i = 1 + 0)
    i = 1
    c = 1
mattygs
  • 112
  • 5
0

i++ and ++i are completely different, i++ is post increment which means evaluate i in the expression and then increment once its evaluated. ++i means increment then evaluate the expression. I see in your example you set i = ++i/i++, this is undefined behavior as mentioned in a comment.

ann
  • 576
  • 1
  • 10
  • 19
Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
0

The C99 standard says explicitly (6.5, p2)

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.

The expressions i = ++i; and i = i++; both update i twice, which is not allowed.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203