6. Constants
Constants are values in a program which do not change. We will be using two types of constants in our programs: literals and symbolic constants.
Literals
Literals are data values we incorporate into our code. Because they are typed as part of our code and do not change when a program runs, they are sometimes referred to as “hard-coded” values. Here are lists each type of literal with examples:
| Type | Examples | Data Type | Usage Example |
|---|---|---|---|
| Integer constant | 23, -4, 0 | int | int number = 17; |
| Floating-Point constant | 3.5, -.6, 0.0, 9. | double | double amount = 2.75; |
| Character constant | 'h', 'A', '9', '$' | char | char letter = 'y'; |
| String constant | "hi", "123", "$" | std::cout << "Hello"; |
In the Usage Example column, the literals are 17, 2.75, 'y', and "Hello".
Some additional comments:
- Floating-point constants are numbers with decimal points in them. It is not necessary for the number to have any digits after the decimal point.
- The data type of a floating-point constant is
double, notfloat. - A character constant is a single character in single quotes. An exception is the newline sequence (‘
\n’), which is considered a single character. - A string constant is one or more characters in double quotes.
- A digit placed in single or double quotes is considered a character, not a numeric value.
Symbolic Constants
Suppose a program uses the same literal multiple times. For example, a program could calculate a 15% discount on multiple items being purchased where the amount saved on each item is needed. The floating-point constant 0.15 would be used many times in the program.
Now suppose there is a new sale and items are sold at 20% off instead of 15%. In order for the program to continue working correctly, each 0.15 used to calculate the amount saved on an item would need change to 0.20. One way to accomplish this would be to find each 0.15 and manually change it to 0.20. This could work, but it could also be easy to accidentally miss an instance of 0.15.
A second way to make the needed changes would be to perform a global search for 0.15 and replace each value found with 0.20. This approach could also work, but what if 0.15 is used in the program for a second purpose? Perhaps all employees get an extra 15% off all purchases. Accidentally changing the additional 15% off to 20% off would be a mistake.
It would be nice if we had an easy way to change all the values we needed to without accidentally changing those we shouldn’t touch. And we do: symbolic constants.
Definition
A symbolic constant is the association of a name with a specific value. When we write a program using a symbolic constant we use the name of the constant in our code and the value is substituted for us automatically.
The general form of the definition of a symbolic constant is as follows:
#define NAME value
The name of a symbolic constant should be meaningful and in uppercase letters. If the name consists of multiple words, separate the words with an underscore character. The value, a literal to be represented by the name, should follow it. Note that neither an equal sign nor a semicolon is used. Place symbolic constant definitions above main() and under the #include statements.
The definition of a symbolic constant for a 15% discount rate appears below:
#define DISCOUNT_RATE 0.15
How Symbolic Constants Work
Symbolic constants are used at the time a program is compiled. Prior to your code being translated to machine language, the name of the symbolic constant is searched for and replaced with the value with which it is associated. The definition of the symbolic constant is then deleted. Continuing with the example above, the text DISCOUNT_RATE would be searched for and replaced with 0.15 wherever found. The definition of the symbolic constant, as shown above, would then be deleted and the program translated to machine language.
The usefulness of symbolic constants can be seen when the value in the definition changes. Suppose 0.15 needed to change to 0.20. To make the change, simply replace 0.15 in the symbolic constant definition with 0.20 and recompile the program. Now DISCOUNT_RATE will be replaced with 0.20. No instance of DISCOUNT_RATE will be missed nor will a 0.15 be changed which should not be.
Additional Points
- The replacement of a name with its associated value takes place in RAM. Your physical source code file is not changed.
- Since all instances of the name are replaced, and the definition deleted, the symbolic constant does not exist in the machine language version of the program.
- Symbolic constants are not variables. Memory is not allocated into which the value is stored.
- If a symbolic constant’s name is used in a string constant, it will not be replaced by the value.
As can be seen, much of a symbolic constant’s usefulness is in allowing us to make a single change which is then automatically applied multiple times to the remainder of a program. There is, however, the opinion among some programmers that symbolic constants should be used for all literals. Each literal would then have a meaningful name which could aid in understanding a program.
A Sample Program
The program below demonstrates the use of a symbolic constant. The program reads the price of an item and then calculates its cost using a sales tax rate of 6.75%.
// Program : calculate_cost.cpp
// Author : A. Student
// Due Date : September 9, 2025
// Description: This program asks the user to enter a price
// and calculates the total amount owed using a
// sales tax rate of 6.75%.
#include <iostream>
#include <iomanip>
#define TAX_RATE 0.0675 // sales tax rate
int main()
{
double price, // stores the price of the item entered
total_owed; // stores the calculated amount owed
std::cout << "How much does the item cost? ";
std::cin >> price;
total_owed = price * (1 + TAX_RATE);
std::cout << std::fixed << std::setprecision(2);
std::cout << "Please pay $" << total_owed << std::endl;
return 0;
}