O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required