+ 3
Output Please Explain it
int i=3; i=++i+i++; cout<<i; outputs 9 This question is in the challenges section. I got it wrong.đđđ also from my knowledge the answer should be compiler dependent because different compilers interpret it in different ways as my brain's compiler interpreted output to be 8đđđ
5 Answers
+ 3
I thought post increment operator uses value then increases it.
Also I thought c++ compiler compiles left to right and not from right to left so first post increment evaluates and then pre increment.
Feel free to correct me if I am wrong at any point.
+ 2
i = ++i + i++; // i = 3
i = 4 + 4; // since its ++i, so its incremented to 4(Pre-increment). Now i is 8.
cout << i; // It will print 9 because, compiler will do the post-increment which is there in previous statement.
Which is not like i = 4 + 5, then there will not be any difference between pre and post increments.
C++ evaluates from left to right, so first pre-increment will be done and use the value for calculation and store it to the left side variable. After that only post increment will be performed.
Hope you are clear now....
+ 2
No still not clear.
according to what you wrote
int i=3
i=i++;
cout<<i;
should output 4 but outputs 3.
+ 1
I dont have confidence on what should be performed after ++i = 4. there are 2 ways:
assignment i = 4+4 or post-increment i = 5.
looks like both: assignment and increment.
But it doesnt works for 10 + i++ when we get 13
0
i=++i+i++;
i=4+i++; //++i=4 and then i=4
i=4+5; //i++=5 and then i=5
i=9;