+ 1

Prefix & Postfix Increment operators

#include <iostream> using namespace std; int main() { int a=3,b=2; b=a++; //after this value of b will be 4 cout<<++b; //here the value getting printed should be 5 but the output is 4 return 0; }

30th Apr 2018, 4:21 AM
Cyan
Cyan - avatar
1 Réponse
0
Great example to explain the difference of prefix and postfix increment! b = a++ will assign the value of a to b and then increment a. So b will be 3, but a will be 4. cout << ++b will first increment b and then print its new value: 4.
4th Jul 2022, 7:53 PM
Chris
Chris - avatar