Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Constants

A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value. You must initialize a constant when it is created. C++ has two types of constants: literal and symbolic.

A literal constant is a value typed directly into your program wherever it is needed. For example, consider the following statement:

long width = 5;

This statement assigns the integer variable width the value 5. The 5 in the statement is a literal constant. You can't assign a value to 5, and its value can't be changed.

The values true and false, which are stored in bool variables, also are literal constants.

A symbolic constant is a constant represented by a name, just like a variable. The const keyword precedes the type, name, and initialization. Here's a statement that sets the point reward for killing a zombie:

const int KILL_BONUS = 5000;

Whenever a zombie is dispatched, the player's score is increased by the reward:

playerScore = playerScore + KILL_BONUS;

If you decide later to increase the reward to 10,000 points, you can change the constant KILL_BONUS, and it will be reflected throughout the program. If you were to use the literal constant 5000 instead, it would be more difficult to find all the places it is used and change the value. This reduces the potential for error.

Well-named symbolic constants also make a program more understandable. Constants often are fully capitalized by programmers to make them distinct from variables. This is not required by C++, but the capitalization of a constant must be consistent because the language is case sensitive.

Defining Constants

There's another way to define constants that dates back to early versions of the C language, the precursor of C++. The preprocessor directive #define can create a constant by specifying its name and value, separated by spaces:

#define KILLBONUS 5000

The constant does not have a type such as int or char. The #define directive enables a simple text substitution that replaces every instance of KILLBONUS in the code with 5000. The compiler sees only the end result.

Because these constants lack a type, the compiler cannot ensure that the constant has a proper value.

Enumerated Constants

Enumerated constants create a set of constants with a single statement. They are defined with the keyword enum followed by a series of comma-separated names surrounded by braces:

enum COLOR { RED, BLUE, GREEN, WHITE, BLACK };

This statement creates a set of enumerated constants named COLOR with five values named RED, BLUE, GREEN, WHITE and BLACK.

The values of enumerated constants begin with 0 for the first in the set and count upwards by 1. So RED equals 0, BLUE equals 1, GREEN equals 2, WHITE equals 3, and BLACK equals 4. All the values are integers.

Constants also can specify their value using an = assignment operator:

enum Color { RED=100, BLUE, GREEN=500, WHITE, BLACK=700 };

This statement sets RED to 100, GREEN to 500, and BLACK to 700. The members of the set without assigned values will be 1 higher than the previous member, so BLUE equals 101 and WHITE equals 501.

The advantage of this technique is that you get to use a symbolic name such as BLACK or WHITE rather than a possibly meaningless number such as 1 or 700.

  • + Share This
  • 🔖 Save To Your Account