3

Let's have this piece of code:

int a = 1;
int b = ++a + ++a;

In C++ (VS 2010) the result is: b = 6 but in C# the result is: b = 5

What's going on there? Why are the results different?

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
PavelS
  • 260
  • 1
  • 8
  • Are there any C# experts who can say whether or not b = 5 is, in any way, standard? You can see that it's plausible: the central + is commutative; one ++a is 2 the other is 3 and you're summing them. – Bathsheba Jun 18 '13 at 09:38
  • 1
    @Bathsheba Some interesting cross-language discussion here: http://stackoverflow.com/questions/6457130/pre-post-increment-operator-behavior-in-c-c-java-c-sharp – Daniel Daranas Jun 18 '13 at 09:56
  • @Daniel Daranas; great link; I think this question has run its course now. – Bathsheba Jun 18 '13 at 10:00
  • For more detailed C# discussion, see http://stackoverflow.com/q/8573190/96780. – Daniel Daranas Jun 18 '13 at 10:03

3 Answers3

10

It's undefined behaviour in C++. You are trying to modify value more than one time without sequence points (per C++98/03 standards).

About C++11

The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

Examples:

i = v[i++]; // the behavior is undefined
i = i++ + 1; // the behavior is undefined

ForEveR
  • 55,233
  • 2
  • 119
  • 133
7

In C++, int b = ++a + ++a is undefined behaviour so you can expect any result.

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

C# and C++ are different languages, with different semantics.

C# decides to first execute first one ++a, then the other ++a and finally the addition of these two expressions, hence the result is 5.

In C++ you have undefined behaviour. The result could be 2, 3, 4, 5, 6, 34500 or any other. Another possible outcome is Matthew Watson drinking all the beer in his fridge. Anything can happen, actually.

It doesn't make sense, in general, to expect the same behaviour from two different languages. Each one follows its own rules.


Note: See this question Pre & post increment operator behavior in C, C++, Java, & C# for further cross-language discussion.

Community
  • 1
  • 1
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • Dude, the C# result is 5, not 6. – Bathsheba Jun 18 '13 at 09:38
  • And +1 for bringing C# into the answer. But is the C# version guaranteed for all future versions of C#? – Bathsheba Jun 18 '13 at 09:40
  • @Bathsheba I'm too lazy to look it up, but I guess the sequential order of evaluation between the two ++a is well defined. One last thing: in any case, I despise such coding style. – Daniel Daranas Jun 18 '13 at 09:42
  • I find prefix/postfix operators so low-level that I naturaly expected their behaviour to be the same across all languages. But apparently it's not the case. – PavelS Jun 18 '13 at 10:49