+ 6

Can anyone explain the output..?

include<stdio.h> int main() { int x=1; x=( ++x)+(++x)+x; printf("%d", x) ; } //output =9 And this #include <stdio.h> int main() { int x=10; x+=x++ + ++x +x; printf("%d",x); //Output =46 return 0; }

17th Mar 2019, 5:07 AM
Shruti Bansal
Shruti Bansal - avatar
9 Answers
+ 7
Sheldon Duncan You really went out of your way to write an answer, but the truth is that everything containing multiple assignments/increments is undefined in C. The result is not predictable and may depend on the compiler
17th Mar 2019, 5:49 AM
Anna
Anna - avatar
+ 6
Sheldon Duncan While undefined behavior and sequence points apply to C/C++, this is not the case with other languages like Java and C#. That might help explain the disconnect.
17th Mar 2019, 6:01 AM
David Carroll
David Carroll - avatar
+ 4
(2) In the second main method, x is instantiated to 10. You then have x += x++ + ++x + x, which can be changed to x = x + x++ + ++x + x, since x += x is another way of saying x = x + x. However, we'll keep += the same for reasons I'll explain later. To make things clearer, let's add parentheses around the increments: x += (x++) + (++x) + x The first increment is a post increment, not a pre increment, so it occurs after the rest of the expression is evaluated. The second increment is a pre increment, however, so that x and every x after it has increased by 1. As such, we have: x += (10++) + 11 + 11 However, += actually has lower precedence then the remainder (at least, in Java it does - it's probably also like this in Python). So, the x before the += becomes 11 before the rest is evaluated. Thus, it's now: x = 11 + (10++) + 11 + 11 Which is: x = 43++ Which can be simplified to: x = 44
17th Mar 2019, 5:46 AM
Sheldon Duncan
Sheldon Duncan - avatar
+ 4
Anna that's really interesting. In programming contests for Java, the result and evaluation is expected to be exact. Either I don't get something, or stuff is just different in Java.
17th Mar 2019, 5:54 AM
Sheldon Duncan
Sheldon Duncan - avatar
+ 3
(1) I don't know C, but the fundamental logic is the same in a lot of languages, so I think I can answer this. For the first main method, you have x instantiated to 1. You then increment it by 1 twice in the expression x = (++x) + (++x) + x. However, pre-increment (before-increment) is evaluated on the first x, making it 2, then is evaluated on the second x, making it 3. The last x is now 3, too, making it x = 2 + 3 + 3 = 8.
17th Mar 2019, 5:43 AM
Sheldon Duncan
Sheldon Duncan - avatar
+ 1
1. ++x ; increments x first and assigns x the value of 2, ++x ; increments x first and assigns x the value of 3, now equation looks like this; x = 3 + 3 + 3 2. x++ ; assigns the value of 10 to x, than increments to 11, ++x ; increments x first and assigns x the value of 12 now equation looks like this; x += 10 + 12 + 12 ; (value of x is 12) x = 12 + 34
18th Mar 2019, 8:09 PM
Esref Efe Ozcivelek
Esref Efe Ozcivelek - avatar
+ 1
Good examples for understanding posfix, prefix increments and precedence. Evaluation for right side of statments and assigns for left side of =. I thinks this question, but the second is more dificulty to see. But Esref Efe Ozcivelek explain well. I think he is explained your doubt. I think that you can suggest the examples for quizz factory. Excuse my english. Thanks. Below a code that i use for study this question. Show the 34 in calculation of second: https://code.sololearn.com/cxl7W0WI4H69/?ref=app
19th Mar 2019, 1:42 AM
Walberto