Guys what will be the value of x if m=10 and x=++m + m++ ? Explain also ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Guys what will be the value of x if m=10 and x=++m + m++ ? Explain also ?

14th Feb 2019, 7:58 AM
Nihal Sharma
Nihal Sharma - avatar
6 Answers
+ 6
in java its 22 but im curious why its not 23 how i see it is ++m increments the value of m so now m=11 m++ uses the new value of m = 11 so ++m + m++ = 22 here but then m++ should increment by one giving the value of 23.
14th Feb 2019, 9:26 AM
D_Stark
D_Stark - avatar
+ 6
Wouldn't that be another case of undefined behavior in C(++)? I think some compilers might evaluate the expression to 22 and others to 23 because you can't tell the order in which the expression will be evaluated and whether the increment by m++ takes place before or after the result is assigned to x.
14th Feb 2019, 9:49 AM
Anna
Anna - avatar
+ 2
21
14th Feb 2019, 8:01 AM
eMBee
eMBee - avatar
+ 2
Well in JavaScript it shows 22 but in c or c++ it's 23.
14th Feb 2019, 9:27 AM
Nihal Sharma
Nihal Sharma - avatar
+ 1
Interesting!!!
14th Feb 2019, 9:32 AM
Ulisses Cruz
Ulisses Cruz - avatar
0
Yeah that's what i was thinking.
14th Feb 2019, 9:49 AM
Nihal Sharma
Nihal Sharma - avatar