0

Does it have something to do with order of execution of cout statement?

Code:

int main() {

int x=80;
int &y=x;
cout<<x<<" "<<(--y)<<endl;
return 0;
}

Output: 79 79

Timisorean
  • 1,388
  • 7
  • 20
  • 30
Kishore
  • 53
  • 6

2 Answers2

3

Does it have something to do with order of execution of cout statement?

This has everything to do with the order of evaluation. Until c++17 your example would exhibit undefined behaviour:

If a side effect on a scalar object is unsequenced relative to a value computation using the value of the same scalar object, the behavior is undefined.

Since c++17 the behaviour is defined and the correct output in this case is

80 79
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37
2

It's undefined behavior, meaning anything could happen.

For more information see: Unexpected order of evaluation (compiler bug?)

Ihor Morenets
  • 29
  • 2
  • 4