-1

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

There was a very nice question on Stack overflow.

For i = 0, why is (i += i++) equal to 0?

But when I tried out the same code in C, it gave different results:

int i = 0;          
i += i++;          // 1 in C and 0 in C#
printf("%d", i);

But the following:

i = i++ + i;       // 1 in C and 1 in C#
i += i++ + i;      // 1 in C

In C# it evaluates the ++ and =+ operators, first by assigning tempVar for each fo them and doing the operation on the tempVars. How does C implements it? Or is different by architecture?

Community
  • 1
  • 1
zeronone
  • 2,912
  • 25
  • 28
  • 7
    Because undefined behavior is undefined? – Xymostech Nov 28 '12 at 02:25
  • 1
    The C++ standard, in their infinite wisdom, made the order expressions are evaluated undefined. – Robert Cooper Nov 28 '12 at 02:26
  • Yep, the behavior is undefined. It's not specified whether the `+=` or the `++` will be evaluated first. It could vary even within a single compiler. – Hot Licks Nov 28 '12 at 02:27
  • Because Microsoft dictated that that's how C# behaves while in C or C++, there is no such rule forced down the implementers' throat. – shinkou Nov 28 '12 at 02:28
  • 1
    @Hot Licks: "It's not specified whether the += or the ++ will be evaluated first." --- actually it's not a problem at all. The operators precedence clearly states `++` will be evaluated first – zerkms Nov 28 '12 at 02:28
  • 1
    @zerkms - Not really. Precedence is not the same as evaluation order. – Hot Licks Nov 28 '12 at 02:34
  • The question is confusing. The title says `i = i++`, but the actual test case is `i = i++ + i` (where it really is just undefined behavior). I'm going to edit the title. – zneak Nov 28 '12 at 02:35
  • @zerkms Could you in future formulate that differently? There are so many people led to believe that operator precedence determines order of evaluation that your comment may exacerbate the misconception. It's the fact that `i++` is one operand of `+=` that requires `i++` to be evaluated before `+=`. – Daniel Fischer Nov 28 '12 at 02:36
  • 1
    @zneak `i = i++` is also just undefined behaviour. – Daniel Fischer Nov 28 '12 at 02:37
  • @Daniel Fischer: the formulation is perfectly clear. In this case undefined behaviour is caused by a sequence point (two modifications between 2 sequence points) not operator precedence. "It's the fact that i++ is one operand of += that requires i++ to be evaluated before +=" --- it's the fact because of operator precedence, nothing else. – zerkms Nov 28 '12 at 02:40
  • @Daniel Fischer: "i = i++ is also just undefined behaviour." --- right, and again it still has nothing to do with "it's not specified what operator `=` or `++` is evaluated first", it's again caused by 2 value modifications between sequence points. – zerkms Nov 28 '12 at 02:41
  • @zerkms Yes. And I assumed that you knew that. But I have seen the idea that evaluation order were directly determined by operator precedence (e.g. that in `x && y++` the `y++` must be evaluated first because of precedence) so often here, that I fear your statement may deepen such confusion. If you don't omit the mention of the grouping that is determined by precedence, I hope that would be less amenable to deepen the confusion. – Daniel Fischer Nov 28 '12 at 02:45
  • @zerkms The other thing was a reply to zneak's "but the actual test case is `i = i++ + i` (where it really is just undefined behavior)", not to you. – Daniel Fischer Nov 28 '12 at 02:46
  • @Daniel Fischer: "The other thing was a reply to zneak's" --- oh, okay. I'm used to seeing my nickname misspelled so often so I reply to anything started with `z` lol )) – zerkms Nov 28 '12 at 02:47

1 Answers1

0

The C standard does not specify an order of evaluation. It is left to the compiler implementation.

igon
  • 3,016
  • 1
  • 22
  • 37
  • Order of evaluation of what exactly? – zerkms Nov 28 '12 at 02:30
  • As you said in your comment, it is not known whether += or ++ is evaluated first – igon Nov 28 '12 at 02:33
  • That's not important, because in the test case, the expression is `i++ + i` (contrary to what is stated in the question title). Which is evaluated first _is_ undefined. – zneak Nov 28 '12 at 02:34
  • @igon: I didn't say that. It's known which is evaluated first - `++`, because of operator precedence. Not an answer? – zerkms Nov 28 '12 at 02:43
  • I guess, although I see that the question has been closed... – igon Nov 28 '12 at 02:47
  • It is not just unspecified, the C standard explicitly says that the result is undefined. Having an undefined part in your program makes it invalid, and anything at all can happen. – Bo Persson Dec 05 '12 at 19:08