02 Constants
02 Constants
Literals
Literals are the most obvious kind of constants. They are used to express
particular values within the source code of a program. We have already used some in
previous chapters to give specific values to variables or to express messages we
wanted our programs to print out, for example, when we wrote:
a = 5;
Integer Numerals
1776
707
-273
These are numerical constants that identify integer values. Notice that they are
not enclosed in quotes or any other special character; they are a simple succession
of digits representing a whole number in decimal base; for example, 1776 always
represents the value one thousand seven hundred seventy-six.
In addition to decimal numbers (those that most of us use every day), C++ allows
the use of octal numbers (base 8) and hexadecimal numbers (base 16) as literal
constants. For octal literals, the digits are preceded with a 0 (zero) character.
And for hexadecimal, they are preceded by the characters 0x (zero, x). For example,
the following literal constants are all equivalent to each other:
75 // decimal
0113 // octal
0x4b // hexadecimal
These literal constants have a type, just like variables. By default, integer
literals are of type int. However, certain suffixes may be appended to an integer
literal to specify a different integer type:
Unsigned may be combined with any of the other two in any order to form unsigned
long or unsigned long long.
For example:
75 // int
75u // unsigned int
75l // long
75ul // unsigned long
75lu // unsigned long
In all the cases above, the suffix can be specified using either upper or lowercase
letters.
3.14159 // 3.14159
6.02e23 // 6.02 x 10^23
1.6e-19 // 1.6 x 10^-19
3.0 // 3.0
These are four valid numbers with decimals expressed in C++. The first number is
PI, the second one is the number of Avogadro, the third is the electric charge of
an electron (an extremely small number) -all of them approximated-, and the last
one is the number three expressed as a floating-point numeric literal.
Suffix Type
f or F float
l or L long double
For example:
3.14159L // long double
6.02e23f // float
Any of the letters that can be part of a floating-point numerical constant (e, f,
l) can be written using either lower or uppercase letters with no difference in
meaning.
'z'
'p'
"Hello world"
"How do you do?"
The first two expressions represent single-character literals, and the following
two represent string literals composed of several characters. Notice that to
represent a single character, we enclose it between single quotes ('), and to
express a string (which generally consists of more than one character), we enclose
the characters between double quotes (").
Both single-character and string literals require quotation marks surrounding them
to distinguish them from possible variable identifiers or reserved keywords. Notice
the difference between these two expressions:
x
'x'
Character and string literals can also represent special characters that are
difficult or impossible to express otherwise in the source code of a program, like
newline (\n) or tab (\t). These special characters are all of them preceded by a
backslash character (\).
For example:
'\n'
'\t'
"Left \t Right"
"one\ntwo\nthree"
Several string literals can be concatenated to form a single string literal simply
by separating them by one or more blank spaces, including tabs, newlines, and other
valid blank characters. For example:
Note how spaces within the quotes are part of the literal, while those outside them
are not.
Some programmers also use a trick to include long string literals in multiple
lines: In C++, a backslash (\) at the end of line is considered a line-continuation
character that merges both that line and the next into a single line. Therefore the
following code:
x = "string expressed in \
two lines"
is equivalent to:
All the character literals and string literals described above are made of
characters of type char. A different character type can be specified by using one
of the following prefixes:
Note that, unlike type suffixes for integer literals, these prefixes are case
sensitive: lowercase for char16_t and uppercase for char32_t and wchar_t.
For string literals, apart from the above u, U, and L, two additional prefixes
exist:
Prefix Description
u8 The string literal is encoded in the executable using UTF-8
R The string literal is a raw string
In raw strings, backslashes and single and double quotes are all valid characters;
the content of the literal is delimited by an initial R"sequence( and a
final )sequence", where sequence is any sequence of characters (including an empty
sequence). The content of the string is what lies inside the parenthesis, ignoring
the delimiting sequence itself. For example:
R"(string with \backslash)"
R"&%$(string with \backslash)&%$"
Both strings above are equivalent to "string with \\backslash". The R prefix can be
combined with any other prefixes, such as u, L or u8.
Other literals
Three keyword literals exist in C++: true, false and nullptr:
true and false are the two possible values for variables of type bool.
nullptr is the null pointer value.
We can then use these names instead of the literals they were defined to:
[demo]
#include <iostream>
using namespace std;
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * pi * r;
cout << circle;
cout << newline;
}
[/demo]
31.4159
Edit & Run
For example:
[demo]
#include <iostream>
using namespace std;
#define PI 3.14159
#define NEWLINE '\n'
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
[/demo]
31.4159
Edit & Run
Note that the #define lines are preprocessor directives, and as such are single-
line instructions that -unlike C++ statements- do not require semicolons (;) at the
end; the directive extends automatically until the end of the line. If a semicolon
is included in the line, it is part of the replacement sequence and is also
included in all replaced occurrences.