2
int a = 1;
a += ++a;
cout << a << endl; // 4
int a = 1;
a += a++;
cout << a << endl; // 3

why these two exmaples have difference behaviour?

火狐狸
  • 193
  • 1
  • 4
  • `++a` means the variable will increase by one before every other things. but `a++` means when everything is done, your variable will increase. in this case, first step will be `a+a` which both values are `1` and equals to `2`. then `a` will increase to `3` – prhmma Dec 21 '19 at 15:23
  • Related: [What is x after “x = x++”](https://stackoverflow.com/questions/7911776/what-is-x-after-x-x). – Jaideep Shekhar Dec 21 '19 at 15:27
  • 7
    Please take care that your examples only have well-defined behavior since C++17 (and later). Pre-C++17 the second example has undefined behavior and pre-C++11 so does the first, see [Undefined behavior and sequence points](https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – walnut Dec 21 '19 at 15:35

6 Answers6

4

Warning: the assignments you are looking at have undefined behaviour. See Why are these constructs using pre and post-increment undefined behavior? Also this answer in the question Undefined behavior and sequence points addresses how C++17 resolves these issues.

It is possible that your compiler processes the operations in the following way, explaining the differences:

pre-increment operation: The following lines:

a=2;
a+=++a;

are equivalent too:

a=2;
tmp=++a;    
a+=tmp;

which reads as:

  1. assign 2 to the variable a
  2. pre-increment a (++a) giving a the value 3 and assign it to tmp (making it 3)
  3. increment the value of a (currently 3) with the value of tmp (currently 3) giving us 6

post-increment operation: The following lines:

a=2;
a+=a++;

are equivalent too:

a=2;
tmp=a++;    
a+=tmp;

which reads as:

  1. assign 2 to the variable a
  2. post-increment a (a++) first returns the original value of a (i.e. 2) and assigns it to tmp (making it 2) and then increments a to the value of 3
  3. increment the value of a (currently 3) with the value of tmp (currently 2) giving us 5
kvantour
  • 25,269
  • 4
  • 47
  • 72
  • Any comments from any of the C++ guru's are always welcome. I'm here to learn – kvantour Dec 21 '19 at 16:52
  • See @walnut's coment on the original question: before 17 and 11 resp, those are undefined expressions with literally no meaning whatsoever. After that, your "equivalent" bits sort of okay, but it would be more correct to talk about sequencing, as in, += now has to wait and not read from a on the left until ++ finishes writing into a on the right. – Cubbi Jan 14 '20 at 15:15
3

a++ and ++a do different things.

a++ increases a by 1 and returns previous value .

++a increases a by 1 and returns new value.

ALX23z
  • 4,456
  • 1
  • 11
  • 18
0

a++ returns the value of a before the increment. ++a returns the value of a after the increment. That is why they are different.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

The difference here is not the priority of the three operators. It follows this chart where a++ > ++a > a+=.

But rather the way the two increment operators work. When you use the ++ayou first increment and then return that value (the new one.) Where as using a++ will first use the old value and then increment. Also see here for related.

Rietty
  • 1,116
  • 9
  • 24
0
a += ++a;

Replaced with compiler like below :

a = a + 1;// a = 1 + 1
a = a + a;// a = 2 + 2 

In second example

a += a++;

is resolved as below :

a = a + a;// a = 1 + 1
a = a + 1;// a = 2 + 1

Below are the rules for operation: https://en.cppreference.com/w/cpp/atomic/atomic/operator_arith2 https://en.cppreference.com/w/cpp/language/operator_precedence

Build Succeeded
  • 1,153
  • 1
  • 10
  • 24
  • Wrong explanation and wrong link, but if you follow it, it has the correct link right under Notes – Cubbi Jan 14 '20 at 15:08
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – kvantour Jan 14 '20 at 15:41
  • Improved the answer – Build Succeeded Jan 14 '20 at 16:44
  • Still wrong. The link you need to read is https://en.cppreference.com/w/cpp/language/eval_order – Cubbi Jan 23 '20 at 14:55
  • I think the question was answered in a descriptive way. Mentioned links are references lead to correct behavior. Whats wrong with the links or answer? So, I can improve. My thought behind writing the answer is not to spoonfeed each thing. People should understand actual approach to solve problem then code will improve automatically. What you say? – Build Succeeded Jan 24 '20 at 05:42
0

With:

a += a++;

you add the previous value of a, and then increment a by 1

With:

a += ++a;

You add the value already incremented by 1 of a

Math Lover
  • 148
  • 10