+ 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.
19 Respuestas
+ 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++;
+ 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.
+ 3
Adding a link to add some context, and also a link to second what John Wells said.
https://stackoverflow.com/questions/7911776/what-is-x-after-x-x
https://softwareengineering.stackexchange.com/questions/153386/why-is-x-x-undefined
+ 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).
+ 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 !!
0
Hatsy Rei
oh i understood that what you said but what about x=++x
0
Hatsy Rei
but if x = x++ is nothing in c++ so if i write this command the result must be error !!
0
「HAPPY TO HELP」 The boss can answer this 😁😁 So you can
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
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
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
0
nope during x++, value of x is the same increment takes place on the next command
0
Sreejith
so after the x++ command it increment ?
0
yes , it increments on next command
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
0
this makes sense
check it out, but it's for java
https://stackoverflow.com/questions/226002/whats-the-difference-between-x-x-vs-x
0
in java you have to try ++x instead x++
0
「HAPPY TO HELP」
So i am smarter than computer ?
0
Thanks you So much for help
and i want to tell you that i am a human 😎😎 what about you ?