0

When using a single cout to print the same variable updated multiple times, I am getting a weird order of updates. Can anybody explain how such updates are done?

 int value = 2;
 cout << value << value++ << ++value << endl; // 434  
 value = 2;
 cout << ++value << value++ << value << endl; // 424 
 value = 2;
 cout << value++ << value++ << ++value << endl; // 435
 value = 2;
 cout << ++value << value++ << value++ << endl; // 532
Micky6789
  • 43
  • 5

1 Answers1

0

The order in which expressions in a single statement are executed is undefined. Obviously unless specified via parenthesis or rules of order of execution. For example:

int a[3]{};
int i=1;
a[i] = i++; //undefined if a[1] or a[2]

Behaviour of such code is not defined and depends on compiler and platform in use. Needless to say you should not rely on a certain behaviour of this code.

Klaus
  • 538
  • 8
  • 26