0

training in this game

can you tell me in which worl isn't this five? int a=3; int b=2; b=a++; cout << b++;

16th Jul 2016, 4:05 AM
Klára Oborná
Klára Oborná - avatar
6 ответов
+ 2
its answer is 4
16th Jul 2016, 5:19 AM
Karan Luther
Karan Luther - avatar
+ 1
but the compiler read the code from right to left so it increment b and then print it
16th Jul 2016, 5:10 AM
Klára Oborná
Klára Oborná - avatar
0
Basically you ignore the b=2. b=a++ is 4, the b++ is done after it prints to the screen. so the answer wouldn't be 5 unless you called b again
16th Jul 2016, 5:01 AM
Allen Cox
Allen Cox - avatar
0
okay. I'm going to try to explain this because it took me a LOT of effort to figure this out, and I want to clear this up so, you have this code, I won't bother explaining the first part, I assume the pre/post increments are what is confusing: b=a++ the ++ doesn't matter, because when you put the '++' after the identifier, it uses the value of a, then changes it. So b will equal a, then it adds 1 to a. also, then you have (assuming you meant b++, not ++b): cout << b++ the postfix with the ++ after means that it outputs the value of b, THEN increments. in reality, the answer should be 3 if you followed me on this. I hope this clears up any questions you may still have.
16th Jul 2016, 5:07 AM
Autumn S
Autumn S - avatar
0
i just rewrite the training from this game without include namespace etc
16th Jul 2016, 5:11 AM
Klára Oborná
Klára Oborná - avatar
0
What value did it return, instead of 5 as you predicted?
16th Jul 2016, 5:14 AM
Autumn S
Autumn S - avatar