0

I was practicing increment operator with its two varieties prefix and postfix. For fun, I wrote the following program which has left me puzzled. The main body of the program is as follows:

int a=10;
a=a++; 
cout << a; 

  1. The output I got is 10. First I thought it's true, as the assignment is done first and then the value of a is incremented by 1. So yes output should be 10 as it is shown.

  2. But the seconds later I thought it should be 11. Because when the second statement is completed, even though a was assigned to 10, immediately we also incremented it by 1. So the output should be 11. What's going wrong here? Thanks.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
Believer
  • 261
  • 1
  • 8
  • Why it is undefined? Is it not a legal c++ statement? – Believer Jul 15 '20 at 11:57
  • 3
    @SamiKuhmonen UB until the advent of the C++17 standard. – Adrian Mole Jul 15 '20 at 11:57
  • Before C++17, it was undefined behaviour, since it modifies `a` twice in one statement. A compiler is not required to diagnose undefined behaviour. – Peter Jul 15 '20 at 12:02
  • Possibly a better dupe-target if C++17 is involved: [C++ 17 post increment operation](https://stackoverflow.com/q/59633725/10871073). – Adrian Mole Jul 15 '20 at 12:02
  • @Adrian Mole I read top voted answer and have following doubt. When second statement is executed, my a is assigned to 10 and incremented by 1. So is it true that my variable is holding two values at a time? – Believer Jul 15 '20 at 12:25
  • In the C++17 standard, the RHS of the assignment operation is first evaluated, **along with all its consequences**. This will give a 'result' of 10 (stored in a temp) and then increment `a`. After ***all*** that is done, the assignment is carried out: thus overwriting the modified (incremented) `a` value. 10 will be the well-defined answer. – Adrian Mole Jul 15 '20 at 12:28
  • @Adrian Mole Thanks. One more doubt. I just added following lines of code after last statement. ``` int j; j=i; cout << j;``` It is again giving me 10. What happened to that increment? – Believer Jul 15 '20 at 12:33
  • Not sure what `i` is - but if you meant `j = a` then the value of `a` as determined already will be used. The increment is completely lost. – Adrian Mole Jul 15 '20 at 12:51
  • @Adrian Mole Ok. Thanks. (Sorry that i instead of a was typo) – Believer Jul 15 '20 at 17:25

2 Answers2

3

You can think of the code like this:

int a = 10;
int temp = a++;
a = temp;
cout << a;

Both the left and right hand sides of an assignment have to be calculated before the actual assignment, which means the side effect from incrementing it will be calculated before the assignment (see this answer). So, what's really happening is that a++ returns 10, then increments a to 11, but then a is being set to that old value of 10, so nothing happens in the end.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • Using **clang-cl** with the standard set to C++14 gives this: **warning : multiple unsequenced modifications to 'a' [-Wunsequenced]**. But using C++17 gives no warning (and nor should it). – Adrian Mole Jul 15 '20 at 12:11
  • A little doubt. In my code, when I declared ```int a; ```compiler created some room to store value of variable a. Now when next statement is executed, my a is assigned to 10 and incremented by 1. So is it true that my variable is storing two values at a time? – Believer Jul 15 '20 at 12:23
  • `a++` creates a temporary value with the old value of the variable. That's why it's recommended to use `++a` when you don't need to use the value. – Aplet123 Jul 15 '20 at 12:25
1

In your code:

int a=10;//assign value

a=a++;//assign then increase means a = 10 (intermediatly a=11) but it assign value before that.

cout << a; //print
Ashish Karn
  • 1,127
  • 1
  • 9
  • 20