+ 3

x = x++ ?

Hi all, I really need help at this: #include <iostream> int main ( ) { int x; x=10; x=x++; std::cout << x; return 0; } // outputs 10 The question is, why isn't the output 11? I know that i used postfix increment operator, but at least of this command the value of x increase. So shouldn't it be at least 11? I know that if i use x++ without x= that it will increase but I want to understand.

11th May 2018, 12:46 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
19 odpowiedzi
+ 11
x = x++ does nothing in C++, apparently nothing in Java as well. IIRC, the original value of x is returned to the RHS of the statement, x is incremented (thanks to postfix increment), and then the statement is executed, which assigns the old value (RHS) back to x. Simply do x++ or ++x instead of x = x++;
11th May 2018, 12:52 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Anytime you use ++ or -- to modify a variable and use that variable more than once in the statement, you have undefined behavior. The compiler can freely choose which order things occur so you can't count on getting any result. Today's answer might be different than tomorrow's, after a compiler update.
11th May 2018, 1:04 PM
John Wells
John Wells - avatar
11th May 2018, 1:46 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
This is the definition of undefined behavor. It is impossible to know what is going to happen today or after the next compiler update. Today, the sequence happens to be: Save the current x (10) in register (A). Save the current x (10) in second register (B). Add 1 to register B. Store register B into x (putting 11 in). Store register A into x (replacing 11 with 10). Tomorrow, it might be: Save the current x (10) in register (A). Save the current x (10) in second register (B). Store register A into x (putting 10 in). Add 1 to register B. Store register B into x (replacing 10 with 11).
11th May 2018, 1:42 PM
John Wells
John Wells - avatar
+ 1
「HAPPY TO HELP」 My brain will explode if x++ = x+1 So x=x++ = x=x+1 and thats right and thats right to me and i have another problem that i am arab so i cant understand everything you say i really didnt understand because the lanuage is big problem !!
11th May 2018, 3:48 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
Hatsy Rei oh i understood that what you said but what about x=++x
11th May 2018, 12:57 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
Hatsy Rei but if x = x++ is nothing in c++ so if i write this command the result must be error !!
11th May 2018, 1:01 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
「HAPPY TO HELP」 The boss can answer this 😁😁 So you can
11th May 2018, 1:02 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
let's break it down x=4 x++ <- at this instant x is 4 cout << x <- now x is 5 so when you do x = x++ it's like assigning 4 to x again after incrementing nornally, it is done like this y=5 y = x++ <- here y=5, x=5 cout << y <- here y=5 cout << x <- here x=6 i hope this clears your doubt
11th May 2018, 1:09 PM
‎ ‏‏‎Anonymous Guy
0
John Wells I really dont think that it undfined behavior because as simply the least of command the value inrease so it has to be 11
11th May 2018, 1:11 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
Sreejith the postfix command 《x++》 assign The value to x THEN increment so its like : x=4 x = x++ x = x now x = 4 then ++ so x = 5 thats i am talking about
11th May 2018, 1:16 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
nope during x++, value of x is the same increment takes place on the next command
11th May 2018, 1:19 PM
‎ ‏‏‎Anonymous Guy
0
Sreejith so after the x++ command it increment ?
11th May 2018, 1:20 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
yes , it increments on next command
11th May 2018, 1:21 PM
‎ ‏‏‎Anonymous Guy
0
Sreejith so if i write cout command after it it has to be 11 not 10 look at my question and code i wrote the cout after x++ statment so the output must be 11
11th May 2018, 1:23 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
11th May 2018, 1:41 PM
‎ ‏‏‎Anonymous Guy
0
in java you have to try ++x instead x++
11th May 2018, 3:07 PM
Anaana Aana
Anaana Aana - avatar
0
「HAPPY TO HELP」 So i am smarter than computer ?
11th May 2018, 5:11 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
Thanks you So much for help and i want to tell you that i am a human 😎😎 what about you ?
11th May 2018, 7:16 PM
Mohammad Alshareef
Mohammad Alshareef - avatar