+ 20
Why in Java result = 8, but in C result = 9
https://code.sololearn.com/c8y6W18Vlw1A/?ref=app https://code.sololearn.com/c05gmsGROhPU/?ref=app
41 Respuestas
+ 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=app
• https://code.sololearn.com/ck689G7hHxg9/?ref=app
• https://code.sololearn.com/cdiP8gSPdSTi/?ref=app
+ 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.
+ 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 !!!
+ 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.
+ 9
Anna
Thanks. Very detailed answer:-)
+ 6
Sonic this question appear with help SoloLearn challenge. In Java I answer 9 (as in C), but there was answer 8
+ 6
UraL The answer in C isn't 9 though. The answer in C is undefined behavior
+ 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
+ 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
=================
+ 5
Sonic it was in SoloLearn
+ 4
@Anna I think in C is correct
+ 4
Although all depend on compiler
+ 4
With or without brackets the result in C is 9
+ 3
Suzie and what about i++ ?
+ 3
kamlesh patil thanks
+ 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.
+ 3
Srinivasa Karthik Java ignore i++.
+ 3
Cool
+ 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.