+ 2

Can You Explain?

int a = 3 int b = 2 b = a++ cout << ++b outputs 4. so i understand it but i dont REALLY understand it. they say a++ changes changes the value of b to a but doesnt increment till the cout line is ran but ++b increments then changes the value. i dont get it 😅. any help would be fantastic THANKS!!

10th Apr 2017, 1:17 PM
Seth T
Seth T - avatar
5 ответов
+ 21
b gets the value of a (3) and then a is incremented afterwards b is incremented once before output (4) (~_~)
10th Apr 2017, 1:19 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
b=a++ means b=a and a=a+1 cout<<++b means b=b+1 and cout<<b
10th Apr 2017, 1:19 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 3
when u do ++a means the value increments and sets the incremented value to b. b = ++a;// a=a+1; b=a; when you do a++ means the initial value will be stored in b ,the value will be incremented. b = a++;//b=a; a=a+1;
10th Apr 2017, 1:25 PM
Henok
Henok  - avatar
+ 1
yea i know but why does a++ basically just make b inherit the value of a and nothing more? why not just say b = 3?
10th Apr 2017, 2:20 PM
Seth T
Seth T - avatar
+ 1
like why does b=a++ not make b 4? it takes 3 1ST then what, it just completly forgets about the ++ part and doesnt increment it? thats what doesnt make sense.
10th Apr 2017, 5:46 PM
Seth T
Seth T - avatar