-1

This is the unary operator problem I am facing, and I fail to understand why VSCode gives output 8,13 when turbo c gives out the answer 8,15?

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int a;
    a = 2;
    cout<<++a + ++a;              //This is displaying 8 which is understood
    
    a = 2;
    cout<<endl<<++a + ++a + ++a;  //Why is this displaying 13, shouldn't it be 5+5+5=15?
}
  • 4
    Who teaches `#include `? – Evg May 29 '21 at 05:06
  • Related: https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior#949443 – alter igel May 29 '21 at 05:06
  • your code has undefined behavior. – N0ll_Boy May 29 '21 at 05:06
  • You will have to reconsider many of what you did here. Don't be discouraged thought. As to the question, it may give you 8,13 or 8,15, or something else. Because it is undefined behavior. – rawrex May 29 '21 at 05:07
  • Doesn't repro on my machine, because it doesn't compile, because it has unsequenced modifications, and I have warnings-as-errors. I recommend you turn your compiler's warnings on. – Eljay May 29 '21 at 05:11
  • [Why should I not #include ?](https://stackoverflow.com/q/31816095/995714), [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv May 29 '21 at 09:03

0 Answers0