Previous Table of Contents Next


Defining Constants with #define

To define a constant the old-fashioned, evil, politically incorrect way, you would enter:

#define studentsPerClass 15

Note that studentsPerClass is of no particular type (int, char, and so on). #define does a simple text substitution. Every time the preprocessor sees the word studentsPerClass, it puts 15 in the text.

Because the preprocessor runs before the compiler, your compiler never sees your constant; it sees the number 15.

Defining Constants with const

Although #define works, there is a new, better, less fattening, and more tasteful way to define constants in C++:

const unsigned short int studentsPerClass = 15;

This example also declares a symbolic constant named studentsPerClass, but this time studentsPerClass is typed as an unsigned short int.

This takes longer to type, but offers several advantages. The biggest difference is that this constant has a type, and the compiler can enforce that it is used according to its type.

Enumerated Constants

Enumerated constants create a set of constants with a range of values. For example, you can declare COLOR to be an enumeration; and you can define that there are five values for COLOR: RED, BLUE, GREEN, WHITE, and BLACK.

The syntax for enumerated constants is to write the keyword enum, followed by the type name, an open brace, each of the legal values separated by a comma, and finally a closing brace and a semicolon. Here’s an example:

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

This statement performs two tasks:

1.  It makes COLOR the name of an enumeration, that is, a new type.
2.  It makes RED a symbolic constant with the value 0, BLUE a symbolic constant with the value 1, GREEN a symbolic constant with the value 2, and so forth.

Every enumerated constant has an integer value. If you don’t specify otherwise, the first constant will have the value 0, and the rest will count up from there. Any one of the constants can be initialized with a particular value, however, and those that are not initialized will count upward from the ones before them. Thus, if you write

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

then RED will have the value 100; BLUE, the value 101; GREEN, the value 500; WHITE, the value 501; and BLACK, the value 700.

Summary

In this hour you learned about numeric and character variables and constants.

You must declare a variable before it can be used, and then you must store the type of data that you’ve declared correct for that variable. If you put too large a number into an integral variable, it produces an incorrect result.

This chapter also reviewed literal and symbolic constants, as well as enumerated constants, and showed two ways to declare a symbolic constant: using #define and using the keyword const.

Q&A

Q If a short int can run out of room, why not always use long integers?
A Both short integers and long integers will run out of room, but a long integer will do so with a much larger number. However, on most machines a long integer takes up twice as much memory every time you declare one. Frankly, this is less of a problem than it used to be, because most personal computers now come with many thousands (if not millions) of bytes of memory.
Q What happens if I assign a number with a decimal to an integer rather than a float? Consider the following line of code:
int aNumber = 5.4;
A A good compiler will issue a warning, but the assignment is completely legal. The number you’ve assigned will be truncated into an integer. Thus, if you assign 5.4 to an integer variable, that variable will have the value 5. Information will be lost, however; and if you then try to assign the value in that integer variable to a float variable, the float variable will have only 5.
Q Why not use literal constants; why go to the bother of using symbolic constants?
A If you use the value in many places throughout your program, a symbolic constant allows all the values to change just by changing the one definition of the constant. Symbolic constants also speak for themselves. It might be hard to understand why a number is being multiplied by 360, but it’s much easier to understand what’s going on if the number is being multiplied by degreesInACircle.


Previous Table of Contents Next