Solve this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Solve this

a=10 x=++a + --a + a++ + ++a + a++ x=?

24th Apr 2022, 6:11 AM
Himanshu Sharma
Himanshu Sharma - avatar
7 Answers
+ 5
IDE ought to be showing "undefined behaviour, multiple read access and modification without sequence point". In the translation of C to ASM, the order of events is only up to sequence points in the code. Between sequence points, the compiler is free to arrange the ASM instructions anyway it sees fit to optimise for speed or size. Since you operate on `a` 5 times without sequence point (points where C and ASM are sync'ed again), there is no telling what the result is. If you want to know why your compiler in its current version for your OS evaluates this expression to 54, find the switch to compile without assembling (usually the -S switch) and study the assembler code.
24th Apr 2022, 6:40 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
But IDE(dev c) showing answer 54
24th Apr 2022, 6:36 AM
Himanshu Sharma
Himanshu Sharma - avatar
+ 1
line 1, a is not defined; Try it yourself. If done see the answer below 1 + 0 + 0 + 2 + 2 if a = 0; then, Ans: 5 , a = 3
24th Apr 2022, 6:22 AM
Raju Adhikary
Raju Adhikary - avatar
+ 1
The order of operations is: 1. Unary (++, --) 2. Binary (+) Furthermore, when there is "a++" the old value of "a" is taken for the expression and then gets incremented and worked with in the next steps. That's how we get a=10 a = 11 (incremented first, then taken) + 10 (decremented first) + 10 (10 is taken, new value is 11) + 12 (11 is increased first, then taken) + 12 (we take 12 for the expression and then it's set to 13, but that value remains unused) = 55
24th Apr 2022, 6:40 AM
Alexander Velinov
Alexander Velinov - avatar
+ 1
Himanshu Sharma You may also got some Warnings variable "a" may not defined See the following code , you may understand https://code.sololearn.com/c0mMgL7kdmM4/?ref=app
24th Apr 2022, 6:51 AM
Raju Adhikary
Raju Adhikary - avatar
0
11 + 10 + 10 + 12 + 12 Ans. 55 and a= 13
24th Apr 2022, 6:28 AM
Raju Adhikary
Raju Adhikary - avatar
0
x = 55, a =13
24th Apr 2022, 8:17 AM
Sanchayeta Ghosh