+ 3

please explain

What is the output of the following code? int a=3; int b=2; b=a++; cout<<++b; the answer was 4 but why, a plus 1 is 4 if b=4 plus 1 its 5 its 4 just if you don't add the last line ++b

18th Nov 2020, 9:08 PM
Netanel
Netanel - avatar
2 odpowiedzi
+ 10
a = 3 b = a++ //b takes the value 3 because you're using a postfix ++, postfix first return value 3 then increment it to 4 cout << ++b // now you're using prefix, first increment the value b = 3, then return it, b=4
18th Nov 2020, 9:15 PM
CoffeeByte
CoffeeByte - avatar
19th Nov 2020, 3:02 PM
Jayakrishna 🇮🇳