1

how the result of the code below is 2?

int i=2,j; j=i++ + --i; cout<<j;

It is evaluating --i first and then i++,but it should be other way round i.e.(postfix increment having higher precedence should be executed first), so the answer should be 4.

Mikhil Singh
  • 107
  • 5

1 Answers1

2

Actually, no, it is unspecified which side of the + will be evaluated first. Due to this and because those operations act on the same variable, your program has undefined behaviour. Anything can happen. Don't ever write code like this.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055