Literals and Constants in C++ Programming


This document assumes that you are already familiar with different data types and how they are handled in the C++ Programming Language. If you require review in this area, read the web page entitled Data Types in the C++ Programming Language.

LITERALS DEFINED

A literal is any item of data (of any data type) that is the same every time a program is run. For example, if one of the steps in a program involved dividing a number by ten, then the value 10 would be a literal. Literals are also referred to as "constants" because their values do not change each time a program is run, as variables do.

DOCUMENTATION OF LITERALS

Literals require no formal documentation in an analysis. Whenever an analyst wants to refer to a never-changing item of data, he or she simply writes the value, as in: 5 or "William" (a string literal).

LITERAL USE IN C++

A literal can be any type of data. The following items of data are examples of constants.

ValueData Type
5Integer Number
2.7Floating Point Number
'A'Single Character
"Hello"String of Characters

Integers are written as whole numbers without any decimal points. Floating point numbers are written with a decimal point. As such, the value 2.0 would be identified by a compiler as a floating point constant (because of the decimal).

Note that, in C++ language source code, literals that are character-based data must be enclosed in either apostrophes (a.k.a. "single quotes") for a single character or standard quotes (a.k.a. "double quotes") for strings of characters. This is done to prevent the compiler from mistaking them for identifiers (labels such as variables or function names).


NAMED CONSTANTS

Sometimes in programming, values that we expected to be constant end up changing. For example, we might write a program that uses our local tax rate to calculate sales tax. Each time the program is run, the same value (perhaps the rate 0.06) will be used. So that value would be a constant. We might have many steps in our program that use that constant. So we would type the literal 0.06 at many positions within the source code. If we ever needed to change the tax rate for that program, we would have to locate and revise every place that we typed 0.06 in our code. To prevent all of this work, we could instead store the literal 0.06 in a variable at the beginning of the source code and use the variable's identifier in place of the literal throughout the code to refer to it. The cost of this approach is that it uses up a storage location in memory. The risk of using this approach is that the value stored in the variable could be mistakenly altered, violating its "constant" nature. The solution to this is to designate the variable defined to hold the literal as a "named constant" by using the keyword const ahead of the data type keyword in the defining statement as in:

   const  float  RATE = 0.06;

The statement above defines the identifier RATE as as a "read-only" variable (known in C++ as a "named constant") and assigns the literal 0.06 into it. The identifier RATE can now be used in each position in the source code that we would have typed 0.06. But any attempt to alter the value stored in the named constant after its initial assignment (shown above) would fail.

SYMBOLIC CONSTANTS

An alternative to defining a named constant to represent a literal throughout source code is to employ an older approach used by C Programmers of defining an alias, known as a "symbolic constant", to be used in place of a literal value throughout source code. Unlike with named constants, the use of a symbolic constant does not involve defining a variable and does not use a storage location in memory. Instead the programmer writes a preprocessor directive like the one below to define an identifier that will be used in place of a literal value throughout the source code.

   #define  RATE  0.06

Note that preprocessor directives are not considered to be C++ statements. So they are not terminated with a semicolon. They should be written on their own line in the source code. Also there must be no blank space between the # character and the keyword define. Also there is no equals symbol (=), because there is no assignment taking place here, just an alias definition. The data type of a symbolic constants is implied by the way in which the literal value is typed in the preprocessor directive. In the example above, the literal was typed in the format of a single precision floating point number. So that is the data type of the identifier RATE.

After the directive above is written, a programmer could then use the identifier RATE in place of the literal 0.06 throughout their source code. When the source code is compiled into object code, the compiler reads preprocessor directive and puts the literal value back in each position that we typed the symbolic constant before translating each statement into object code. The result is that the identifier used for the symbolic constant is completely eliminated from the source code and the literal value is translated in each position where it had been. This requires the use of no variable storage space and makes it easy to change the value of the symbolic constant in the future if needed. We simply change the single preprocessor directive at the beginning of the source code (where it is easy to find). Any change to it before our program is compiled will change the literal valued used by the compiler in every statement that mentioned the symbolic constant.

DOCUMENTATION OF NAMED CONSTANTS AND SYMBOLIC CONSTANTS

Because named constants and symbolic constants are arbitrary labels defined by av analyst or programmer, they do require formal documentation in the form of a Symbolic (or Named) Constant List. An example of such a list follows.

IDENTIFIER DESCRIPTION DATA TYPE VALUE USAGE DESTINATION
RATE Sales tax rate Floating point 0.06 for TAX ---

The IDENTIFIER column defines the label to be used as the symbolic constant in the code in place of the value shown in the VALUE column. The DESCRIPTION column defines what the constant represents. The DATA TYPE column indicates the data language in which the data should be represented. The USAGE column indicates any processing that will occur using the value. Finally, the DESTINATION column indicates if the value should be output by the program.

NAMED AND SYMBOLIC CONSTANT USE IN C++

Named and symbolic constants can be used almost anywhere in source code that a literal could be used. If we were writing a program that would normally use the symbol period (.) in many statements, but which might someday use an exclamation mark in those positions instead, we could define a symbolic constant (MARK in the example below) to represent that character as follows.

   #define  MARK  '.'

Then when it had to be changed, we would simply revise the single preprocessor directive line above to read as shown below and recompile the source code.

   #define  MARK  '!'

Note that the choice of the identifier MARK is completely arbitrary. You could use any valid C++ identifier that had not already been used in the code. Also note the use of the single quotes (apostrophes) around the character constant and the lack of a semi-colon at the end of this line.

If we were writing a program that would normally use the string (phrase) "The answer is:" in many statements, but which might someday use the phrase "Result =" in those positions instead, we could define a constant macro to represent that string as follows:

   #define  MESSAGE  "The answer is:"

Note that again the choice of the identifier MESSAGE is completely arbitrary. You could use any valid C++ identifier that had not already been used in the code. Also note the use of the double (standard) quotes around the string constant.

PATH: Instructional Server> COP 2000> Examples>