0

Dummy question

What is the output of the following code? int a=3; int b=2; b=a++; cout<<++b; I dont undestand why the answer is 4 If b = a++ that means // b= 2 cout << ++b; // that shood mean 2+1 right ?

17th Sep 2017, 5:44 PM
Costin Bucuresteanu
Costin Bucuresteanu - avatar
4 Respuestas
+ 9
b = a++ means "b takes a's value and a is incremented by 1" so after that a would be 4 and b would now be 3 (a's value before it was incremented) and the ++b would be 4
17th Sep 2017, 5:51 PM
LynTon
LynTon - avatar
0
b gets the value of a and b is 3, then a increments by 1 and a is now 4.In next statement value of b, which is incremented by 1 is printed and then, the output is 4. I understand partially Why do we declare int b=2; wat is the point there dont get me wrong but the question is from the Quiz beginers is this code like this b=2; b=a++; (As statement of b=2?) then I search for result what is b? and if a++ = 3-1 and result b=2 then b = 3 and b++ = 3+1 ? is this corect?
17th Sep 2017, 6:33 PM
Costin Bucuresteanu
Costin Bucuresteanu - avatar