Increments help please! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Increments help please!

help! why does this spit out "2 1" and not "1 2" ? #include <iostream> using namespace std; int main() { int x=1; cout <<x++ << " " <<x++; return 0; }

23rd Feb 2017, 1:21 AM
tou xiong
tou xiong - avatar
2 Answers
0
There's no sequence point with the << operator This will give u the expected answer. #include <iostream> using namespace std; int main() { int x=1; cout <<x++; cout<<" "; cout << x++; return 0; } try below links. http://stackoverflow.com/questions/2129230/cout-order-of-call-to-functions-it-prints http://stackoverflow.com/questions/10778627/why-is-i-i-1-undefined-behavior-in-c11
23rd Feb 2017, 3:01 AM
Eranga
Eranga - avatar
0
In my opinion...it is called Cascading of Operators. When we use two or more shift operators (<<), then the processing starts from the end of the statement. Thus, in your question case, since the processing starts from left... the value of right one is 1 and the left one is 2. Since it is displayed from left to right (as usual)... hence the answer is: 2 1. Hope this helped you.:)
25th Feb 2017, 4:22 PM
Devansh Kaushik
Devansh Kaushik - avatar