+ 5

Why output of this code is 10? int main() { int a=4,b; b=a+ ++a; cout<<b; return 0; }

26th Jul 2017, 3:04 PM
Nargiza Eshbekova
Nargiza Eshbekova - avatar
30 Respostas
+ 5
answer is 10 since a++ gets incremented first and then value is assigned to a
30th Jul 2017, 4:12 AM
Komal Laghate
+ 4
bogdan you are correct šŸ‘
29th Jul 2017, 1:31 PM
Vivek Singh
Vivek Singh - avatar
+ 4
no, read it like this, add a to a, but increment value of a by 1 before evaluating this expression.
30th Jul 2017, 5:51 AM
Vivek Singh
Vivek Singh - avatar
+ 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
26th Jul 2017, 4:34 PM
Saumya
Saumya - avatar
+ 3
if i write "a+ ++a" it gives 10, if "a+ + +a"=8, if "a+++a" and "a++ +a" it gives 9
26th Jul 2017, 4:51 PM
Nargiza Eshbekova
Nargiza Eshbekova - avatar
27th Jul 2017, 8:53 AM
Saumya
Saumya - avatar
+ 3
@Komal but 4+(4+1)= 9
30th Jul 2017, 5:30 AM
Saumya
Saumya - avatar
+ 3
@saumya incremented value of a will be considered so 5+(4+1)
30th Jul 2017, 6:57 AM
Komal Laghate
+ 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...
27th Jul 2017, 5:52 AM
Bogdan Sass
Bogdan Sass - avatar
+ 2
How it can be possible @Bogdan can you prove that.
27th Jul 2017, 8:47 AM
Irwin Lopez
Irwin Lopez - avatar
+ 2
I will find out
27th Jul 2017, 11:58 AM
Irwin Lopez
Irwin Lopez - avatar
+ 2
there is Reason behind everything.
27th Jul 2017, 12:01 PM
Irwin Lopez
Irwin Lopez - avatar
+ 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. "
27th Jul 2017, 2:06 PM
Bogdan Sass
Bogdan Sass - avatar
+ 2
Thank you @Irwin@Bogdan@Komal@Vivek
30th Jul 2017, 10:25 AM
Saumya
Saumya - avatar
+ 2
Sorry IrwinšŸ˜…šŸ˜…
30th Jul 2017, 11:28 AM
Saumya
Saumya - avatar
+ 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.
26th Jul 2017, 4:08 PM
Irwin Lopez
Irwin Lopez - avatar
+ 1
@Irwin - I did run the code, and the result is 10. Check your spacing: a + ++a is different from a++ + a...
26th Jul 2017, 5:48 PM
Bogdan Sass
Bogdan Sass - avatar
+ 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.
27th Jul 2017, 10:30 AM
Bogdan Sass
Bogdan Sass - avatar
+ 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. :)
27th Jul 2017, 1:39 PM
Bogdan Sass
Bogdan Sass - avatar
+ 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.
30th Jul 2017, 9:47 AM
Bogdan Sass
Bogdan Sass - avatar