how these expressions are going to get calculated? [why?] | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 3

how these expressions are going to get calculated? [why?]

a += a++; (in C/C++) a += a++; (in Java)

21st May 2019, 3:09 PM
kiRA
kiRA - avatar
8 Antworten
+ 7
There's a huge difference in the final outcome of such an expression when it gets compiled and executed in C/C++ and Java JVM. Firstly, in a C or C++ compiler ¹ the expression `a += a++` is considered ambiguous because there's no way for the implementation (compiler) to figure out in which order it must get evaluated and the side-effect of arithmetic operations get applied to the same variable in a single expression (breaking the sequencing rule) ². In a technical term, the value that is going to be stored in `a` is undefined ³ (unpredictable). However, in Java, the order of evaluation is well-defined and it's guaranteed that the result is something that the programmer expected ⁴. _____ ¹ http://cpp.sh/3piok ² https://en.wikipedia.org/wiki/Sequence_point ³ https://en.wikipedia.org/wiki/Undefined_behaviorwww.jdoodle.com/a/1dQ6
21st May 2019, 4:19 PM
Babak
Babak - avatar
+ 20
The value of the left-hand side is saved at the start, and then added to the value of right hand side. Expression evaluation in Java is left-to-right, the modification of a caused by a++ is overwritten by the subsequent assignment. This means that a += a++; is equivalent to a += a;
23rd May 2019, 8:48 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 9
Also, If you wonder how the value of `a` becomes 2 in Java, note that the effect of post-increment is completely ignored there. Here is an interesting discussion about that: https://stackoverflow.com/questions/7911776/what-is-x-after-x-x
21st May 2019, 5:08 PM
Babak
Babak - avatar
+ 6
In C(++) the behavior will be undefined
21st May 2019, 3:34 PM
Anna
Anna - avatar
+ 6
A quick look at Java SE reference: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.7.1 It turned out the expression `x += x += -2` gets evaluated like so: First, the value of the left-most x (7) being saved, then addition operation performs on the second x and -2 which give us the value of 5. Finally, 5 and 7 get added together and we will have 12 as our final result. x += (x += -2);
21st May 2019, 7:28 PM
Babak
Babak - avatar
+ 5
V__ believing the false evidences is detrimental in the world of programming. I encourage you to take 2 minutes of your time and read about the simplest possible explanation about undefined behavior. It absolutely worth your while and prevents technical humiliation in more formal discussions. http://c-faq.com/ansi/undef.html " undefined: Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended."
23rd May 2019, 4:33 AM
Babak
Babak - avatar
+ 3
C++ Soldier (Babak) thank you for your answers. the real reason why i started this discussion because of this question(in java) int x=7; x+=x+=-2; i thought answer would be 10 but its 12 actually
21st May 2019, 6:45 PM
kiRA
kiRA - avatar
+ 3
C++ Soldier (Babak) seems like UB is the evil gripping programmers work and you are the exorcist lol
23rd May 2019, 7:48 AM
AZTECCO
AZTECCO - avatar