Increment and Decrement Operators

As you program, you'll often encounter situations in which you need to add or subtract one to or from a number. Quite frequently this occurs within loops, as you'll see in Chapter 6, “Control Structures.” For this reason, C has the increment and decrement operators:

int a = 10;
a++; /* 11 */
a++; /* 12 */
a--; /* 11 */

These operators come in both the postfix (shown above) and prefix (below) versions, differing only in precedence (see the next section of the chapter for more):

int a = 10;
--a; /* 9 */
++a; /* 10 */

When assigning the value of an incremented or decremented variables to another variable, which form you use makes all the difference, as the following example will demonstrate.

To use the increment ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.