+ 2

[DUPLICATE] x++ and ++x?

int a=3; int b=2; b=a++; //here b assign 3+1? cout<<++b; then the output is why not 1+4? i guess the output is 5 but it's 4. plz explain this, i'm beginner and dumb at programming.

26th Mar 2018, 12:09 PM
AungLwin MinnHtet
5 Answers
+ 8
++x (prefix op) -> increments i and returns the value of i after the increment x++ (postfix op) -> increments i and returns the value of iĀ beforeĀ the increment Here b=a++; Here value of b is 3 and will be 4. cout<<++b; Here value of b is 4
26th Mar 2018, 12:17 PM
Vidya
Vidya - avatar
26th Mar 2018, 3:09 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 2
b=3 and then a becomes 4 is what b=a++; does.
26th Mar 2018, 12:10 PM
John Wells
John Wells - avatar
+ 2
a++ only increments a, not b
26th Mar 2018, 12:29 PM
Chris
Chris - avatar
+ 1
x++ means first do what you have to do then add one to the variable. ++ x means the opposite, that is, before anything else add one variable. at first I was also confused by this (in JavaScript) but then I got used to it. OBS: the same goes for the --.
26th Mar 2018, 1:28 PM
J G
J G - avatar