How to Use C++ Constants

Working with Constant Values in C++

© Mark Alexander Bain

Feb 21, 2009
Creating Constants with C++, Mark Alexander Bain
C++ constants are values used in a program and will not change during the operation of the program and are important for any programmer in the operation of their programs

C++ constants are not very different from any C++ variable. They are defined in a similar way and have the same data types and the same memory limitations. However, there is one major difference - once a constant has been created and value assigned to it then that value may not be changed.

Defining Constants with C++

There are actually three ways of defining a constant in a C++ program:

  • by using the preprocessor
  • by using the const key word
  • by using enumerators - these will have a range of integer values

It's also worth noting that there are two types of constant: literal and symbolic.

Literal and Symbolic Constants

A literal constant is simply a value used directly in a block of code, for example:

circumference = 2 * 3.14 * r;
area = 3.14 * r * r;
Here 2 and 3.14 (pi to 2 decimal places) are literal constants. However, there is an obvious disadvantage to using literal constants. If the constant needs to be changed (for example changing pi to 4 decimal places) then the programmer will have to manually change all instances of the constant themselves. It is therefore often more practical to use symbolic constants:
circumference = 2 * pi_val * r;
area = pi_val * r * r;
Now the value of the constant only needs to be defined once. Defining C++ Constants with the Preprocessor The preprocessor is run before the program is compiled. It will look for any lines starting with a hash (#) and will then modify the code according to what it finds. An example of a constant defined in the preprocessor is:
#define pi_val 3.1416
When the preprocessor runs it will search the code for all instances of pi_val and replace it with the number 3.1416. Defining C++ Constants with the Const Key Word C++ constants can also be defined in the same way as C++ variables. The only difference is that the const key word is required:
const float pi_val = 3.1416;
One important advantage of using the const key word to define constants is that the data type will always be enforced. Defining Enumerated Constants An enumerated type is a series of named integer values created by using the enum key word, for example:
enum DAY {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
In this example SUNDAY will have the value 0 and SATURDAY will be 6. However, the values can be initialized:
enum DAY {MONDAY = 1, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};
This time MONDAY will be 1 and SUNDAY will be 7. The use of enumerators is clarified by seeing a working example:
#include
enum DAY {MONDAY = 1, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};
int main () {
int day;
cout << "Enter day number: ";
cin >> day;
if ((day == SATURDAY) || (day == SUNDAY)) {
cout << "Weekend"; }
else if ((day >= MONDAY) && (day <= FRIDAY)) {
cout << "Working Day";
} else {
cout << "Invalid Number";
}
return 0;
}
If 1 - 5 are input as the day number then the program will output "Working Day" or "Weekend" for 6 or 7. Summary Constants are used in a C++ program to store information that will not change while the program is running. They can be one of two general types:
  • literal
  • symbolic
These are defined by:
  • using the #define key word in the preprocessor
  • using the const key word
  • creating enumerators
These give the C++ programmer a great deal of versatility when it comes to creating the data that they need for their programs to run correctly.

The copyright of the article How to Use C++ Constants in C Programming is owned by Mark Alexander Bain. Permission to republish How to Use C++ Constants in print or online must be granted by the author in writing.


Creating Constants with C++, Mark Alexander Bain
A C++ Program with Enumerators, Mark Alexander Bain
Running and Compliling a C++ Program, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo