In the prefix version (i.e., ++i), the value of i is incremented, and the value of the expression is the new value of i. So basically it first increments then assigns a value to the expression.
In the postfix version (i.e., i++), the value of i is incremented, however, the {value|the worth} of the expression is that the original value of i. So basically it first assigns a value to expression and then increments the variable.
Let's look at some code to get a better understanding −
#include<iostream> using namespace std; int main() { int x = 3, y, z; y = x++; z = ++x; cout << x << ", " << y << ", " << z; return 0; }
This would give us the output −
5, 3, 5
Why is this? Let's look at it in detail −