Defining Arrays Using Constants

In the quiz example, the number of array elements has been set by either using a literal value (answers[3]) or by letting C make that determination when values are assigned. Each method works, but it's allowed—even recommended—that you simplify this process by using a constant to represent the number of array elements.

Constants were introduced in Chapter 2, “Introduction to Data Types.” You'll recall that C supports two formats for constant definitions. The first is to use the const keyword before defining the variable:

const int NUM = 10;
float gpas[NUM];

The second method is to define the constant using a preprocessor directive outside of the main function:

#define NUM 10

You'll learn more about preprocessor directives in Chapter 8, “Using the C Preprocessor.”

Using constants to set the number of array elements makes sense in situations where you might access that value (the array's dimension) multiple times—for example, once when declaring the array and again when processing through all of the array elements in a loop. In this and the next section of the chapter you'll create such an example. By using constants for the array's size, if you later need to change the size, you will need to edit just this one value.

To use constants with arrays

1.
Create a new file or project in your text editor or IDE.

2.
Type the standard beginning lines of code (Script 6.4):

/* exchange.c - Script 6.4 */
#include <stdio.h>
int main (void) {

Script 6.4. A good use of a constant is as a placeholder for the number of elements in an array.


3.
Create a constant:

const int NUM = 10;

This constant is an integer named NUM. You don't have to use all uppercase letters for constant names, but that is a standard convention. The constant is given an initial value of 10, which means that the array that uses this value in its declaration will have 10 elements.

If you'd like to assign more descriptive names, you could call this constant something like ARRAY_SIZE.

4.
Define an array, using the constant to dictate the number of elements:

float amounts[NUM];

Instead of using a literal value (like 10), this array has NUM elements in it. When the application is compiled, NUM will be replaced by NUM's value (10). The array itself is a float type, as it will be holding dollar amounts. You could define amounts as a double, if you want to be more precise.

5.
Complete the main function:

  getchar();
  return 0;
}

6.
Save the file as exchange.c, compile, and debug as necessary (Figure 6.5).

Figure 6.5. Successful compilation of the exchange application, using the gcc compiler.


Yet again, this application doesn't do anything in its current form, so there's no reason to execute it.

✓ Tips

  • You could use a variable instead of a constant in this example:

    int NUM = 10;
    int amounts[NUM];
    

    This is referred to as a variable-length array (VLA), because the length of the array is determined by a variable. The benefit of using a constant is that you know the value will not be changed as the application runs.

  • Do not confuse a variable-length array (VLA) with an array that has a varying length. The former uses a variable to set the array's size but has a fixed size. The latter does not have a fixed size but requires memory management (see Chapter 10).


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

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