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 and decrement operators

1.
Create a new, blank text document in your editor or IDE.

2.
Begin by documenting the file (Script 3.2):

Script 3.2. The results of incrementing and decrementing integers depends on whether you use the postfix or prefix version.


/* ones.c - Script 3.2 */

3.
Type the required include line and begin defining the main() function:

#include <stdio.h>
int main(void) {

4.
Define the variables to be used:

int n1, n2;

This application makes use of two simple integers, called n1 and n2.

5.
Set n1's and n2's initial values and print some introductory text:

n1 = 1;
n2 = 1;
printf ("At first, n1 is %d, n2 is
 %d.
", n1, n2);

The n1 variable will be incremented and decremented to modify n2's value but both should be initialized before you print them. To give you a sense of what's happening in the application, the initial values of both will be printed.

6.
Increment and decrement n1, assigning this value to n2, and print the results:

n2 = n1++;
printf ("After n2 = n1++, n1 is %d,
 n2 is %d.
", n1, n2);
n2 = n1--;
printf ("After n2 = n1—, n1 is %d, n2
 is %d.

", n1, n2);

Using the postfix version of incrementation, n2 is assigned the value of n1 plus one, then both values are printed for comparison. Next, the process is repeated using decrementation.

7.
Reset the value of the variables and add some more descriptive information:

n1 = 1;
n2 = 1;
printf ("After resetting, n1 is %d,
 n2 is %d.
", n1, n2);

To avoid confusion, both variables will be assigned the value of 1 before the prefix operators are used.

8.
Use the prefix version of both operators and print those results:

n2 = ++n1;
printf ("After n2 = ++n1, n1 is %d,
 n2 is %d.
", n1, n2);
n2 = --n1;
printf ("After n2 = —n1, n1 is %d,
 n2 is %d.
", n1, n2);

This step is simply a repeat of Step 6, using the prefix (as opposed to the postfix) operators. The print statements will help to demonstrate the differences when the application is run.

9.
Add the getchar() function to pause execution of the application and finish the main() function:

    getchar();
    return 0;
}

10.
Save the file as ones.c.

11.
Compile and debug the application (Figure 3.3).

Figure 3.3. Using gcc to compile the application on Mac OS X (this would also work on other Unix-based operating systems).


12.
Run the application (Figure 3.4).

Figure 3.4. The results of running the ones application, reflecting how the prefix and postfix versions of incrementing and decrementing n1 affect the value of n2.


As you can see from the results, using the postfix version of each operator, n2 first is assigned the current value of n1 and then n1 is incremented or decremented. Hence, after n2 = n1++, n1 has the value of 2 (1 incremented) but n2 has the value of 1 (n1's value prior to incrementation). Using the prefix versions, n1's value is first altered and then this value is assigned to n2.

✓ Tips

  • C++ gets its name from the notion that it's one notch above C.

  • On the one hand, using the increment and decrement operators creates smaller, faster code. On the other hand, it can make the code harder for a human to follow, so be judicious in its use.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset