+ 5
Why output of this code is 10? int main() { int a=4,b; b=a+ ++a; cout<<b; return 0; }
30 ответов
+ 5
answer is 10 since a++ gets incremented first and then value is assigned to a
+ 4
bogdan you are correct 👍
+ 4
no, read it like this,
add a to a, but increment value of a by 1 before evaluating this expression.
+ 3
//Ans is 9 not 10
It could be bcoz you 've given only a part of the code --and we are getting 9 as an ans fr the code given above
+ 3
if i write "a+ ++a" it gives 10, if "a+ + +a"=8, if "a+++a" and "a++ +a" it gives 9
+ 3
https://code.sololearn.com/c5hyc9y3rl8J/?ref=app
Getting output as 9
+ 3
@Komal but 4+(4+1)= 9
+ 3
@saumya incremented value of a will be considered so 5+(4+1)
+ 2
https://code.sololearn.com/c9h7QYWInJip/?ref=app
@Irwin - your turn now. Post your code that returns 9 :)
BTW - as I mentioned in a previous post, the behavior in this case is _undefined_. It is ENTIRELY possible for this code to return 9 or 10 (on different compilers, different architectures, etc). This is why you should not do this in real life...
+ 2
How it can be possible @Bogdan
can you prove that.
+ 2
I will find out
+ 2
there is Reason behind everything.
+ 2
I have to say - I really enjoy this conversation :) . And Irwin's attitude of "there is always an explanation, and I need to find it!" is commendable - that's the best way of learning new things!
So I managed to find some more (and hopefully easier to follow) explanations regarding this particular issue:
https://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior
https://stackoverflow.com/questions/34280128/how-do-preincrements-and-postincrements-in-the-same-line-work-in-c
Enjoy the explanations - they are VERY interesting! But please remember NOT to write this kind of code in the real life (when programming for a living, and trying to write code that should be easy to understand and maintain). As one stackoverflow answer very nicely puts it:
"you should never write code with expressions like these. They are usually given as academic examples, sometimes showing that different compilers yield different output. "
+ 2
Thank you @Irwin@Bogdan@Komal@Vivek
+ 2
Sorry Irwin😅😅
+ 1
No the Answer will be not 10 actually it will be 9
I have checked it several time the answer is 9
and I don't think that this program is right actually.
+ 1
@Irwin - I did run the code, and the result is 10. Check your spacing: a + ++a is different from a++ + a...
+ 1
Oh, and @Irwin - in coding, the only "truth" that matters is the one that results from running your code. You saying that "the answer is 9" means that you actually ran that code, and got 9 as a result.
I'm still waiting for the proof - mainly because I'm curious about what circumstances (compiler, architecture, etc) could result in 9.
+ 1
Yes, there is a reason - one I explained in the very first post:
"you get 10 because ++a is evaluated (and changes the value of a) before the addition is evaluated."
If you want the nitty-gritty details, they're in the stackoverflow link. :)
+ 1
A very interesting read on the topic (it was linked to in one of the stackoverflow threads above):
http://www.eskimo.com/~scs/readings/undef.950321.html
It explains very nicely why you should NOT write such code, and generally what type of constructs you should avoid.