0

What am I missing here??

Read the following and pls explain me what im doing wrong : int a = 3; int b = 2; b = a++; cout << ++b; What will be printed out "b" ? My answer is 5. where, b = a++ means, b = 3 + 1; so b = 4 so, cout << ++b; means 1 + 4 = 5 zo it will cout out 5......right???

7th Jan 2017, 12:51 AM
Jarno Korteschiel
Jarno Korteschiel - avatar
3 Antworten
+ 2
nope. a++ is not a+1. Its AFTER the operation , a+1 and ++b is BEFORE the operation
7th Jan 2017, 1:57 AM
Nahuel
Nahuel - avatar
+ 1
no, a in line 3 will be incremented after the line 3, so b gets old value of a which is 3. in line 4 b becomes 4 because incrementation is done prior the execution of line.. a=++x is like x=x+1 a=x a=x++ is like a=x x=x+1
7th Jan 2017, 1:58 AM
Stefan Milosavljevic
Stefan Milosavljevic - avatar
0
b = a++ is a post increment operator. 1. b = a // b = 3 2. a = a + 1 // a = 4 to get the answer of 5, do b = ++a, which is the pre increment operator. 1. a = a + 1 // a = 4 2. b = a // b = 4
7th Jan 2017, 1:56 AM
Nikunj Arora
Nikunj Arora - avatar