+ 1

Problem with compiler logic?

Simple example: int x=10, y=5; cout<<y++ + 2<<endl; cout<<x++ + x; SoloLearnC++ gives 7 and 21; Dev-C++ 4.9.9.2 gives 7 and 20; Problem how the compiler interpret x after operation x++. int x =10; x++ + x; SoloLearnC++ interpret like this: 10 + 11 = 21, x=11; DevC++ interpret: 10 + 10 = 20, x=11;

3rd Dec 2018, 9:19 AM
Viacheslav Voskresenskii
2 ответов
+ 5
"Problem with compiler logic" Have you heard of "Undefined behavior"?! These two expressions break the sequencing rule. y++ + 2 x++ + x "Modifying an object between two sequence points more than once produces undefined behavior.[...] ¹" _____ ¹ https://en.wikipedia.org/wiki/Undefined_behavior
3rd Dec 2018, 9:26 AM
Babak
Babak - avatar
0
So if I play one-on-one with someone, and the system gives me the tasks containing "undefined behavior" like x++ + x, I can just say: bad task. ok)
3rd Dec 2018, 9:45 AM
Viacheslav Voskresenskii