Why in Java result = 8, but in C result = 9 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
10th Jun 2019, 7:29 PM
UraL
41 Answers
+ 16
Precedence and associativity of postfix ++ and prefix ++ are different;  ➝ Precedence of postfix ++ is more than prefix ++, their associativity is also different. Associativity of postfix ++ is left to right   and associativity of prefix ++ is right to left. • https://code.sololearn.com/cZh21tT8Yn4D/?ref=apphttps://code.sololearn.com/ck689G7hHxg9/?ref=apphttps://code.sololearn.com/cdiP8gSPdSTi/?ref=app
11th Jun 2019, 5:50 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 15
UraL The problem is that you're trying to calculate something that cannot be calculated. 9 is one correct result, but it is not THE correct result. If another compiler says that the result is 8, that result is also correct. Same for 7, 10, or whatever other compilers may get as result. EVERYTHING any arbitrary compiler claims to be the correct result is correct because undefined behavior leads to unpredictable, yet formally correct results.
11th Jun 2019, 6:54 AM
Anna
Anna - avatar
+ 10
UraL (with reference to C) if u want 8 as answer do, i = i++ + ++i; I had observed it, just now. Well, I also don't know the answer of your doubt !!!
11th Jun 2019, 2:35 AM
RJ😍
RJ😍 - avatar
+ 9
It's bad practice to have such dubious incrementing in your code which depend on language specific implementations but I agree it's interesting academically.
11th Jun 2019, 4:56 AM
Sonic
Sonic - avatar
+ 9
Anna Thanks. Very detailed answer:-)
11th Jun 2019, 7:45 AM
UraL
+ 6
Sonic this question appear with help SoloLearn challenge. In Java I answer 9 (as in C), but there was answer 8
11th Jun 2019, 5:10 AM
UraL
+ 6
UraL The answer in C isn't 9 though. The answer in C is undefined behavior
11th Jun 2019, 5:12 AM
Anna
Anna - avatar
+ 6
UraL - "The output in C is 9." - "The output in C depends on the compiler." Pick one of them, you can't have both
11th Jun 2019, 5:45 AM
Anna
Anna - avatar
+ 5
public class Program { public static void main(String[] args) { int i = 3; i = ++i + i++; System.out.println(i); } } result = 8 ++3 instantly 4 4++ remains 4 4+4 = 8 stored in i then 4++ value become 5 but in next statement we printing value i but i is having 8 =================
11th Jun 2019, 4:35 AM
sree harsha
sree harsha - avatar
+ 5
Sonic it was in SoloLearn
11th Jun 2019, 5:22 AM
UraL
+ 4
@Anna I think in C is correct
11th Jun 2019, 5:19 AM
UraL
+ 4
Although all depend on compiler
11th Jun 2019, 5:21 AM
UraL
+ 4
With or without brackets the result in C is 9
11th Jun 2019, 6:35 PM
UraL
+ 3
Suzie and what about i++ ?
10th Jun 2019, 10:18 PM
UraL
+ 3
11th Jun 2019, 3:24 AM
UraL
+ 3
@Anna result is 9, but I can't guarantee it on all C compilers. By my opinion it must be 9 definitely. But I am not compiler designer. So programmer should not use such ambiguous in one line. Otherwise he can spend a lot of time to debug program.
11th Jun 2019, 5:58 AM
UraL
+ 3
Srinivasa Karthik Java ignore i++.
11th Jun 2019, 6:46 AM
UraL
+ 3
Cool
11th Jun 2019, 5:30 PM
Justin
Justin - avatar
+ 3
UraL Just because the c compiler takes the new value of i i.e. 4 (because of ++i) therefore making i++ = 5. And yielding a result 9. But java being interpreted takes the same value of i for both i++ and ++i throughout the line, therefore making i 8.
12th Jun 2019, 5:10 AM
AJAY SINGH RANA
AJAY SINGH RANA - avatar