CHAPTER 4
Literals, Variables,
and Constants
Chapter Objectives
By the end of the chapter, readers will be able to:
Understand the types and use of literals.
Explore the role of data types in programming.
Describe some of the key primitive data types found in C++.
Understand the requirements and conventions used in naming identifiers.
Differentiate between initialization and assignment.
Use and interpret an ASCII chart.
Understand the use of constants.
Understand the use of variables.
In this chapter we will explore how to store information in memory so that our program has access to the information when needed. Several other chapters focus on the same issue, but they all require the basic concepts introduced in this chapter.
To explain the difference between literals, variables, and constants, let's look at the mathematical formula to calculate the circumference of a circle: 2πr. The character r is a variable that represents the radius of the circle. The symbol π represents the constant for pi, and the character 2 is a numeric literal. We will refer to this example throughout the chapter.
4.1 Literals
A literal is a value that is interpreted exactly as it is written. In our introductory example of the circle circumference formula, the 2 is a numeric literal. It is not a variable or a constant, as explained later in this chapter, because it has no name associated with it.
There are three types of literals: numeric, character, and string. In C++, a numeric literal is represented exactly as suspected. Examples of numeric literals would include 14, 457, and 3.14. A character literal is a single character enclosed by single quotation marks, such as ‘a’, ‘Z’, and ‘9’. A string literal is composed of multiple characters surrounded by double quotation marks (''). You have already been exposed to string literals in the previous chapters. For example, in the following statement, the string literal would be “Hello World”.
std::cout << “Hello World”;
Table 4.1.1 shows examples of the different types of literals.
In Table 4.1.1, the fourth numeric literal, 0xFF, specifies that the value following the x is a hexadecimal value. Notice that the fifth numeric literal starts with a zero, which designates that the literal is an octal value.
Also notice that in the third example of string literals, “A” is a single character surrounded by double quotation marks. Although perfectly legal, it should be avoided because of the additional overhead required. We will have more to say about this later in the text.
Literal Type |
Examples |
Numeric |
0 |
3.12 |
|
-5 |
|
0xFF |
|
0777 |
|
Character |
’A’ |
'' |
|
String |
“Hello World” |
“Ralph” |
|
“A” |
Section 4.1 Exercises
In the following exercises, find the illegal literals. If the literal is legal, state what type of literal it is.
1. -12.34
2. ‘Hello’
3. “F”
4. “1234”
5. ‘1’
6. A
7. “Marcus”
4.2 Character Escape Sequences
An exception to the rule that literals are interpreted exactly as they are written is an escape sequence. Character escape sequences also violate the rule that a character literal be a single character surrounded by single quotation marks. All escape sequences start with a backslash () followed by one or more characters.
Escape Sequence |
Character Representation |
|
Carriage return and line feed (new line) |
|
Tab (eight characters wide) |
” |
Double quote |
’ |
Single quote |
\ |
Backslash |