Each of the size declarators used in the previous section was a literal. A literal is a value that is written exactly as it is meant to be interpreted. For example, the number 3 is a literal. Its value cannot be anything other than 3. You can’t change the number 3 to have some different value. Accordingly, the number 3 may be used in the following program as the size declarator:
#include <iostream> using namespace std; int main () { int testScore[3]; return 0; }
The size declarator may not be a variable. The following program attempts, unsuccessfully, to use a variable numTests in declaring the size of an array:
#include <iostream> using namespace std; int main ()
{
int numTests;
cout << "Enter the number of test scores:"; cin >> numTests;
int testScore[numTests]; return 0;
}
The result is a compiler error. The compiler will flag the declaration of the array (int testScore[numTests]) and complain that a constant expression was expected.
Note It is possible to declare the size of an array with a variable if you use a different array declaration technique, dynamic memory allocation, which is covered in
Chapter 11.
The term constant is new. A constant is a name that represents the same value throughout a program. That value may be any one you specify. This is different than mathematical constants such as PI, which correspond to a given value.
A constant is the converse of a variable, while a variable is a name that may represent different values during the execution of a program. However, the value of a constant cannot change during the execution of a program.
Declaring an Array
Note While neither a literal nor a constant changes its value during the execution of a program, they are not the same. While a constant is a name that represents a value, a literal is not a name, but instead the value itself.
You may use a constant instead of a literal as a size declarator. The size declarator in the following program uses a constant for the value 3 rather than the literal 3.
#include <iostream> using namespace std; int main ()
{
const int numTests = 3; int testScore[numTests]; return 0;
}
Going back to the definition of a constant, a name that represents the same value throughout a program, the name is numTests, and it represents the value 3. The syntax for declaring a constant is similar to, but not the same as, a syntax for declaring a variable. Each requires a data type (here int) and a variable name (here
numTests) and ends in a semicolon. However, there are two differences.
First, the declaration of a constant must begin with the const keyword. This tells the compiler that you are declaring a constant instead of a variable.
Second, the declaration terminates by assigning the constant a value. You also may assign a variable a value when you are declaring it; you learned in Chapter 3 this is called initialization. However, assigning a variable a value when you declare it is optional. On the other hand, assigning a constant a value when you are declaring it is mandatory; the declaration of the constant will not compile if you don’t, the compiler error being that a constant object must be initialized. The reason is, since you cannot assign a value of a constant after you declare it, the only time you can assign a value to a constant is when
you declare it.
Note The declaration of a constant does reserve memory just as does the declaration of a variable. The difference is that with a constant the value stored at the memory address cannot change during the life of the program.
The following program illustrates that you cannot assign a value of a constant after you declare it:
#include <iostream> using namespace std; int main ()
{
const int numTests = 3;
cout << "Enter the number of test scores:"; cin >> numTests;
int testScore[numTests]; return 0;
}
The result is a compiler error. The compiler will flag the attempt to assign a value to the constant (cin >> numTests) and complain that the stream extraction operator >> cannot have a right-hand operand that is a constant. This is simply another way of saying you can’t assign a value to a constant after you declare it.
Declaring an Array
The following program modifies the previous one by assigning the user input to a variable (so far so good) and then attempting to assign that variable to the constant (not good):
#include <iostream> using namespace std; int main ()
{
const int numTests = 3; int num;
cout << "Enter the number of test scores:"; cin >> num;
numTests = num;
int testScore[numTests]; return 0;
}
Once again, the result is a compiler error. The compiler will flag the attempt to assign a value to the constant (numTests = num); the error message will be different than in the previous example, that “l-value specifies const object.” The 1 in “l-value” is a small L, not the number one, and refers to the value to the left of the assignment operator. This again is another way of saying you can’t assign a value to a constant after you declare it. While you can use a constant instead of a literal array to declare the size of an array, the question remains: why would you go to the trouble of doing so? The reason is that in your code you may need to often refer to the size of the array, not only when declaring it, but also, as shown later in this chapter, when assigning values to, and displaying them from, the array. However, the needs of the program may require you to modify the code to change the size of the array, usually to make it larger. For example, if as a teacher I change my policy from giving three tests to giving five tests, I need to change the size of the testScore array from three to five. If I use the literal number 3, I have to find that number each time it is referred to in the program and change it to 5. Not only is this time- consuming, but the potential exists that I could miss a reference I needed to change. By contrast, if I use a constant, such as const int numTests = 3, then all I need to do is change the 3 to 5 in that one place, and I’m done.
You may be thinking, “Wait a second, you just told me earlier in this chapter that you can’t resize an array.” Yes, you cannot resize an array while the program is running. However, you can change the size of the array in the code, and then recompile the program.
Constants have many uses in addition to signifying the size of an array, and those uses will be covered in this and further chapters of this book.