0

i test this code in visual studio and on gdb online:

int a = 10;
int b = 15;
a = b = a++;
cout << a << " " << b;

in visual studio the output was 11 10 and in gdb the output was 10 10. whay there is a difference? what the correct output?

According to my understanding If given a = b = 6 it is actually:

b = 6
a = b

In our program it is like this:

b = a
a += 1
a = b

Therefore will print 10 10

Itoy
  • 11
  • 4
  • Can you specify a C++ standard you want the answer for? IIRC, it's undefined before C++17 (not sure if it's one of the cases that became defined in C++17). – ShadowRanger Jan 13 '22 at 00:07

1 Answers1

3

10 10 is correct since C++17. The right operand of an assignment operator is sequenced before the left operand -- meaning all evaluations and side-effects associated with the right operand are completed, before evaluation of the left operand begins. Further reading: Undefined behavior and sequence points

Prior to C++17 the behaviour was undefined, see Undefined behavior and sequence points .

M.M
  • 134,614
  • 21
  • 188
  • 335