The expressions x++ and ++x have both a result (meaning) and a side effect.
The result of the expression x++ is the current value of x . A side effect is that the content of x increases by 1.
The result of ++x is the current value of x plus 1. The side effect is the same as above.
Please note that a side effect should not be applied immediately after evaluating an expression; it should only be applied to the next point in the sequence. For example, given the code
x = 1; y = 2; z = ++x + y++;
there is no guarantee that the contents of x will be modified before the y++ expression is evaluated, or even before the result of ++x + y++ assigned to z (neither = nor + operators enter a sequence point). The expression ++x evaluates to 2, but it is possible that the variable x may not contain the value 2 until z is assigned.
It is important to remember that the behavior of expressions like x++ + x++ clearly undefined by the locale; there is no (good) way to predict what the result of the expression will be, or what value x will contain after evaluating it.
Postfix operators take precedence over unary operators, so expressions like *p++ parsed as *(p++) (i.e. you apply the * operator to the result of a p++ expression). Again, the result of the p++ expression is the current value of p , so while (*p++=*q++); Do not skip the first item.
Note that the operand for auto-increment / decrement operators must be an lvalue (essentially an expression that refers to a memory location, so that the memory can be read or modified). The result of the expression x++ or ++x not an lvalue value, so you cannot write things like ++x++ or (x++)++ or ++(++x) . You could write something like ++(*p++) ( p++ not lvalue, but *p++ is), although this will probably make you slap everyone who reads your code.
source share