Arithmetic - The ++ and -- Operators

Chapter chap3 section 7

The final operators considered in this chapter are the "++" and "--" operators. These provide a handy way of incrementing or decrementing the value of a variable, a very common programming requirement. Each operator comes in two flavours known as prefix and postfix. The difference concerns whether the value of the expression is the new value of the variable or the old value of the variable. If the operator precedes the name of the variable then the value of the expression is the new value, otherwise it is the original value. Note that these operators can only be associated with variables, they cannot be associated with constants or expressions. The jingles use and increment or increment and use may be helpful here and, of course, another programming example is called for.

main()
{
	int	x=3;
	int	y=4;
	printf("Value of x++ is %d\n",x++);
	printf("Value of ++y is %d\n",++y);
	printf("Value of y++ is %d\n",y++);
	printf("Value of ++x is %d\n",++x);
	printf("Final value of x is %d, y is %d\n",x,y);
}
Producing the output
Value of x++ is 3
Value of ++y is 5
Value of y++ is 5
Value of ++x is 5
Final value of x is 5, y is 6
Again careful consideration of the output will be rewarded by greater understanding of what is actually going on.

The first statement displays the value of x++. This is the postfix flavour meaning use-and-increment so the value of the expression x++ is the original value (3) but as a side effect of evaluating x++ the value of x is changed to 4, however this will not be apparent until the next use of x.

The next statement displays the value of ++y. This is the prefix or increment-and-use flavour so the value of the expression is the value after incrementation of the original value. The net effect is that the value of y is changed to 5.

The next statement works in exactly the same way as the first statement showing the value of y before performing the incrementation implied by y++. During the execution of the previous statement the value of y had been incremented to 5, it, of course, retains this value when the execution of the next statement starts. After the execution the value of y will be 6.

The final statement takes the current value of x, which had been changed to 4 during the execution of the first statement, and increments it further. The postfix "++" means increment-and-use so the value displayed is 5. The "--" operator is totally analagous to the "++" operator only it decrements rather than increments.

Fairly obviously, the "++" and "--" operators can only be applied to variables (or more properly lvalues).

The "++" and "--" operators are very widely used in programming loops and other iterative constructs which we will be using shortly.

The so-called side-effects associated with both the increment and decrement operators and the assignment operators are often confusing to programmers who are familiar with other languages or who have attended courses on software engineering where such things are deprecated as confusing and mysterious. The widespread use of and reliance on side-effects is, however, very typical of normal C programming. It can, in the wrong hands, give rise to very obscure code, in the right hands in can result in remarkably terse programs that will be small and efficient when compiled.

"++" and "--" are examples of unary operators which means that they are associated with a single operand rather than two operands. Other examples are "+" and "-" used in contexts such as

x = -y

and

y = +z

The unary "+" operator doesn't actually do anything useful, it is there for completeness.


Summary of Arithmetic Operators