-1

I have a piece of code

     #include <bits/stdc++.h>
      using namespace std;
      typedef long long ll ; 
      int main() 
      { 
             ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) ; 
             int i, j;
             i = j = 5;
             j = i++ + (i*10);
             cout<<i<<" "<< j <<" "<<i++;

    }

Output of code for c++ 17 is : 6 65 6 Output of code for c++ 11 is : 7 65 6

Why such a difference ?

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 1
    Does this answer your question? [Why are these constructs using pre and post-increment undefined behavior?](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) – aschepler Apr 16 '20 at 19:06
  • 1
    What compiler(s) are you using, and with what flags? – HolyBlackCat Apr 16 '20 at 19:09

1 Answers1

1

Before C++17, the expression j = i++ + (i*10); exhibited undefined behavior.

In C++17, the language rules were changed to define the behavior of such expressions. (ones that modify a variable and use it in the same expression)

So, depending on your compiler, your platform, the optimization settings that you are using, the phase of the moon, etc, you might get the same answer ... or not.

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45