+ 6
int b = 1; b = b++; Why is b's final value 1 and not 2?
8 ответов
+ 7
b = b++ is similar to:
var tmp = b;
b++;
b = tmp;
So yeah, b ends up staying as 1
Source: http://stackoverflow.com/a/226019
+ 5
yea, but shouldn't b's value be incremented after assignment? Because that's how post increment ++ operator works..
+ 4
Ok, now that makes sense
+ 2
b will assign before it gets incremented. b=b++, means b=1++, first it will assign.
0
because if you use b++, b is returned before it gets incremented.
0
Because if you enter b=b++ the compiler will assign b=b and then it will add 1 with b.. That's why finally it will be 1.. On the other hand if you enter b=++b, then the compiler will add 1 with b first and then it will assign it with b.
happy programming :)
- 1
b++ es not the same as ++b
- 1
if you put "++" the value of b will increase one number