I don't understand the concept of postfix and prefix increment or decrement. Can anyone give a better explanation?
All four answers so far are incorrect, in that they assert a specific order of events.
Believing that "urban legend" has led many a novice (and professional) astray, to wit, the endless stream of questions about Undefined Behavior in expressions.
So.
For the built-in C++ prefix operator,
++x
increments x
and produces (as the expression's result) x
as an lvalue, while
x++
increments x
and produces (as the expression's result) the original value of x
.
In particular, for x++
there is no no time ordering implied for the increment and production of original value of x
. The compiler is free to emit machine code that produces the original value of x
, e.g. it might be present in some register, and that delays the increment until the end of the expression (next sequence point).
Folks who incorrectly believe the increment must come first, and they are many, often conclude from that certain expressions must have well defined effect, when they actually have Undefined Behavior.