+17

Why in Java result = 8, but in C result = 9

https://code.sololearn.com/c8y6W18Vlw1A/?ref=app https://code.sololearn.com/c05gmsGROhPU/?ref=app

6/10/2019 7:29:37 PM

UraL

43 Answers

New Answer

+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.

+14

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

+11

https://www.sololearn.com/Discuss/1838305/?ref=app

+8

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.

+7

Anna Thanks. Very detailed answer:-)

+5

UraL The answer in C isn't 9 though. The answer in C is undefined behavior

+5

UraL - "The output in C is 9." - "The output in C depends on the compiler." Pick one of them, you can't have both

+4

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 =================

+4

Sonic this question appear with help SoloLearn challenge. In Java I answer 9 (as in C), but there was answer 8

+3

Sonic it was in SoloLearn

+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.

+2

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 !!!

+2

@Anna I think in C is correct

+2

Although all depend on compiler

+2

As far as I'm concerned it depends to compiler. The logical output is 8.

+2

+2

With or without brackets the result in C is 9

+2

It's about the sequence points in C. There are many of it. The order if the operations is selected by the compiler. But when the code arrives tona sequence point, only then, you can say that there are no more instructions that can carry sisè effects and the result you have is the final. Google for sequence points for more information. Java executes, from to right, I thing and if I'm nota wrong.

+1

UraL here is your answer https://stackoverflow.com/a/23553721

+1

Suzie and what about i++ ?