Literals
As with variables, there are several types of C++ literals. Remember that a literal does not change. Integer literals are whole numbers that do not contain decimal points. Floating-point literals
EXAMPLE
are numbers that contain a fractional portion (a decimal point with an optional value to the right of the decimal point).
Assigning Integer Literal s
You already know that an integer is any whole number without a decimal point. C++ enables you to assign integer literals to vari- ables, use integer literals for calculations, and print integer literals using the cout operator.
An octal integer liter- al contains a leading 0, and a hexadeci- mal literal contains a leading0x .
A regular integer literal cannot begin with a leading 0. To C++, the number 012 is not the number twelve. If you precede an integer literal with a 0, C++ interprets it as an octal literal. An octal literal is a base-8 number. The octal numbering system is not used much in today’s computer systems. The newer versions of C++ retain octal capabilities for compatibility with previous versions.
A special integer in C++ that is still greatly used today is the base-16, or hexadecimal, literal. Appendix A, “Memory Addressing, Binary, and Hexadecimal Review,” describes the hexadecimal num- bering system. If you want to represent a hexadecimal integer literal, add the 0x prefix to it. The following numbers are hexadecimal numbers:
0x10 0x2C4 0xFFFF 0X9
Notice that it does not matter if you use a lowercase or upper- case letter x after the leading zero, or an uppercase or lowercase hexadecimal digit (for hex numbers A through F). If you write business-application programs in C++, you might think you never have the need for using hexadecimal, and you might be correct. For a complete understanding of C++ and your computer in general, however, you should become a little familiar with the fundamentals of hexadecimal numbers.
Table 4.3 shows a few integer literals represented in their regular decimal, hexadecimal, and octal notations. Each row con- tains the same number in all three bases.
Chapter 4 ♦ Variables and Literals
Table 4.3. Integer literals represented in three bases.
Decimal |
Hexadecimal |
Octal |
---|---|---|
(Base 10) |
(Base 16) |
(Base 8) |
16 |
0x10 |
020 |
65536 |
0x10000 |
0100000 |
25 |
0x19 |
031 |
EXAMPLE
Assigning String Literal s
A string literal is One type of C++ literal, called the string literal, does not have a
always enclosed in double quotation marks.
matching variable. A string literal is always enclosed in double quotation marks. Here are examples of string literals:
“ C++ Pr ogr ammi ng” “ 123” “ “ “ 4323 E. Oak Road” “ x”
Any string of characters between double quotation marks— even a single character—is considered to be a string literal. A single space, a word, or a group of words between double quotation marks are all C++ string literals.
If the string literal contains only numeric digits, it is not a number; it is a string of numeric digits that you cannot use to perform mathematics. You can perform math only on numbers, not on string literals.
The double quotation marks are never considered part of the string literal. The double quotation marks surround the string and simply inform your C++ compiler that the code is a string literal and not another type of literal.
It is easy to print string literals. Simply put the string literals in a cout statement. The following code prints a string literal to the screen:
The following code prints the string literal, C++ By Exampl e .
cout << “ C++ By Exampl e” ;
Examples
- The
following program displays a simple message on-screen. No variables are needed because no datum is stored or calculated.
Chapter 4 ♦ Variables and Literals
/ / Fil ename: C4ST1. CPP
/ / Di spl ay a st r i ng on- scr een.
#i ncl ude <i ost r eam. h> mai n( )
{
cout << “ C++ pr ogr ammi ng i s f un! ” ; r et ur n 0;
}
Remember to make the last line in your C++ program (be- fore the closing brace) a r et ur n statement.
- You
probably want to label the output from your programs. Do not print the value of a variable unless you also print a string literal that describes that variable. The following program computes sales tax for a sale and prints the tax. Notice a message is printed first that tells the user what the next number means.
/ / Fil ename: C4ST2. CPP
/ / Comput e sal es t ax and di spl ay i t wi t h an appr opr i at e message.
#i ncl ude <i ost r eam. h> mai n( )
{
f l oat sal e, t ax;
f l oat t ax_r at e = . 08; / / Sal es t ax per cent age
/ / Det ermi ne t he amount of t he sal e. sal e = 22. 54;
/ / Comput e t he sal es t ax. t ax = sal e * t ax_r at e;
/ / Pr i nt t he r esul t s.
cout << “ The sal es t ax i s “ << t ax << “ \ n” ;
r et ur n 0;
}
EXAMPLE
Here is the output from the program:
The sal es t ax i s 1. 8032
You later learn how to print accurately to two decimal places to make the cents appear properly.
String-Literal Ending s
An additional aspect of string literals sometimes confuses beginning C++ programmers. All string literals end with a zero. You do not see the zero, but C++ stores the zero at the end of the string in memory. Figure 4.1 shows what the string “ C++ Program” looks like in memory.
Null zero
Figure 4.1. In memory, a string literal always ends with 0.
You do not have to worry about putting the zero at the end of a string literal; C++ does it for you every time it stores a string. If your program contained the string “ C++ Pr ogr am” , for example, the com- piler would recognize it as a string literal (from the double quotation marks) and store the zero at the end.
Chapter 4 ♦ Variables and Literals
All string literals end in a null zero (also called binary zero or ASCII zero).
The zero is important to C++. It is called the string delimiter. Without it, C++ would not know where the string literal ended in memory. (Remember that the double quotation marks are not stored as part of the string, so C++ cannot use them to determine where the string ends.)
The string-delimiting zero is not the same as the character zero. If you look at the ASCII table in Appendix C, “ASCII Table,” you can see that the first entry, ASCII number 0, is the null character. (If you are unfamiliar with the ASCII table, you should read Appendix A, “Memory Addressing, Binary, and Hexadecimal Review,” for a brief description.) This string-delimiting zero is different from the from the character ‘ 0’ , which has an ASCII value of 48.
As explained in Appendix A, “Memory Addressing, Binary, and Hexadecimal Review,” all memory locations in your computer actually hold bit patterns for characters. If the letter A is stored in memory, an A is not actually there; the binary bit pattern for the ASCII A (01000001) is stored there. Because the binary bit pattern for the null zero is 00000000, the string-delimiting zero is also called a binary zero.
To illustrate this further, Figure 4.2 shows the bit patterns for the following string literal when stored in memory: “ I am 30” .
String-terminating zero
Figure 4.2. The bit pattern showing that a null zero and a character zero are different.
Figure 4.2 shows how a string is stored in your computer’s memory at the binary level. It is important for you to recognize that the character 0, inside the number 30, is not the same zero (at the bit level) as the string-terminating null zero. If it were, C++ would think this string ended after the 3, which would be incorrect.
EXAMPLE
This is a fairly advanced concept, but you truly have to under- stand it before continuing. If you are new to computers, reviewing the material in Appendix A, “Memory Addressing, Binary, and Hexadecimal Review,” will help you understand this concept.
The length of a string literal does
String Length s
Many times, your program has to know the length of a string. This becomes critical when you learn how to accept string input
not include the null
binary zero.
from the keyboard. The length of a string is the number of characters up to, but not including, the delimiting null zero. Do not include the null character in that count, even though you know C++ adds it to the end of the string.
Examples
- The following
are all string literals:
“ 0” “ C” “ A much l onger st r i ng li t er al ”
- The following table shows some string literals and their
corresponding string lengths.
String |
Length |
---|---|
” C” |
1 |
” 0" |
21 |
” Hel l o” |
5 |
” ” |
0 |
” 30 or anges” 10
Assigning Character Literal s
All C character literals should be enclosed in single quotation marks. The single quotation marks are not part of the character, but they serve to delimit the character. The following are valid C++ character literals:
‘ w’ ‘ W’ ‘ C’ ‘ 7’ ‘ * ’ ‘ =’ ‘ . ’ ‘ K’
Chapter 4 ♦ Variables and Literals
C++ does not append a null zero to the end of character literals.
You should know that the following are different to C++.
‘ R’ and “ R”
‘ R’ is a single character literal. It is one character long, because all character literals (and variables) are one character long. “ R” is a string literal because it is delimited by double quotation marks. Its length is also one, but it includes a null zero in memory so C++ knows where the string ends. Due to this difference, you cannot mix character literals and character strings. Figure 4.3 shows how these two literals are stored in memory.
Figure 4.3. The difference in memory between ‘ R’ as a character literal and “ R” as a string literal.
All the alphabetic, numeric, and special characters on your keyboard can be character literals. Some characters, however, can- not be represented with your keyboard. They include some of the higher ASCII characters (such as the Spanish Ñ). Because you do not have keys for every character in the ASCII table, C++ enables you to represent these characters by typing their ASCII hexadecimal number inside single quotation marks.
For example, to store the Spanish Ñ in a variable, look up its hexadecimal ASCII number from Appendix C, “ASCII Table.” You find that it is A5. Add the prefix \ x to it and enclose it in single quotation marks, so C++ will know to use the special character. You could do that with the following code:
char sn=’ \ xA5' ; / / Put s t he Spani sh Ñ i nt o a var i abl e call ed sn.
EXAMPLE
This is the way to store (or print) any character from the ASCII table, even if that character does not have a key on your keyboard.
The single quotation marks still tell C++ that a single character is inside the quotation marks. Even though ‘ \ xA5’ contains four characters inside the quotation marks, those four characters repre- sent a single character, not a character string. If you were to include those four characters inside a string literal, C++ would treat \ xA5 as a single character in the string. The following string literal,
“ An accent ed a i s \ xA0”
is a C++ string that is 18 characters, not 21 characters. C++ interprets the \ xA0 character as the á, just as it should.
Any character preceded by a backslash, \ , (such as these have been) is called an escape sequence, or escape character. Table 4.4 shows some additional escape sequences that come in handy when you want to print special characters.
Table 4.4. Special C++ escape-sequence characters.
Escape Sequence Meaning
\ a Alarm (the terminal’s bell)
\ b Backspace
\ f Form feed (for the printer)
continues
Chapter 4 ♦ Variables and Literals
Table 4.4. Continued.
Escape Sequence Meaning
\ n Newline (carriage return and line feed)
\ r Carriage return
\ t Tab
\ v Vertical tab
\ \ Backslash (\ )
\ ? Question mark
\ ’ Single quotation mark
\ ” Double quotation mark
\ 000 Octal number
\ xhh Hexadecimal number
\ 0 Null zero (or binary zero)
EXAMPLE
Examples
- To print two
names on two different lines, include the \ n
between them.
Print the name Harr y ; drop the cursor down to a new line and print Jerr y .
cout << “ Harr y\ nJerr y” ;
When the program reaches this line, it prints
Har ry Jer ry
You also could separate the two names by appending more of the cout operator, such as:
cout << “ Harr y” << “ \ n” << “ Jerr y” ;
Because the \ n only takes one byte of storage, you can output it as
a character literal by typing ‘ \ n’ in place of the preced- ing “ \
n” .
- The
following short program rings the bell on your com- puter by assigning the \ a escape sequence to a variable, then printing that variable.
/ / Fil ename: C4BELL. CPP
/ / Ri ngs t he bell
#i ncl ude <i ost r eam. h> mai n( )
{
char bell =’ \ a’ ;
cout << bell ; / / No newli ne needed her e. r et ur n 0;
}
Chapter 4 ♦ Variables and Literals