5
in this piece of code was a literal constant.
Literal constants can be classified into: integer, floating-point, characters, strings, Boolean, pointers, and user-defined literals.
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:
1
2
3
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:
u
or U
unsigned
l
or L
long
ll
or LL
long long
unsigned long
or unsigned long long
.
For example:
1
2
3
4
5
75 // int
75u // unsigned int
75l // long
75ul // unsigned long
75lu // unsigned long
e
character (that expresses "by ten at the Xth height", where X is an integer value that follows the e
character), or both a decimal point and an e
character:
1
2
3
4
3.14159 // 3.14159
6.02e23 // 6.02 x 10^23
1.6e-19 // 1.6 x 10^-19
3.0 // 3.0
The default type for floating-point literals is double
. Floating-point literals of type float
or long double
can be specified by adding one of the following suffixes:
f
or F
float
l
or L
long double
1
2
3.14159L // long double
6.02e23f // float
e
, f
, l
) can be written using either lower or uppercase letters with no difference in meaning.
1
2
3
4
'z'
'p'
"Hello world"
"How do you do?"
'
), 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'
x
alone would refer to an identifier, such as the name of a variable or a compound type, whereas 'x'
(enclosed within single quotation marks) would refer to the character literal 'x'
(the character that represents a lowercase x letter).
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 (\
).
Here you have a list of the single character escape codes:
Escape code Description\n
newline \r
carriage return \t
tab \v
vertical tab \b
backspace \f
form feed (page feed) \a
alert (beep) \'
single quote ('
) \"
double quote ("
) \?
question mark (?
) \\
backslash (\
)
'\n'
'\t'
"Left \t Right"
"one\ntwo\nthree"
\
) followed by the code expressed as an octal (base-8) or hexadecimal (base-16) number. For an octal value, the backslash is followed directly by the digits; while for hexadecimal, an x
character is inserted between the backslash and the hexadecimal digits themselves (for example: \x20
or \x4A
).
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:
1
2
"this forms" "a single" " string "
"of characters"
1
"this formsa single string of characters"
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:
1
2
x = "string expressed in \
two lines"
1
x = "string expressed in two lines"
char
. A different character type can be specified by using one of the following prefixes:
Prefix Character type u
char16_t
U
char32_t
L
wchar_t
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:
u8
The string literal is encoded in the executable using UTF-8 R
The string literal is a raw string
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:
1
2
R"(string with \backslash)"
R"&%$(string with \backslash)&%$"
"string with \\backslash"
. The R
prefix can be combined with any other prefixes, such as u
, L
or u8
.
true
, false
and nullptr
:
true
and false
are the two possible values for variables of type bool
.nullptr
is the null pointer value.1
2
3
bool foo = true;
bool bar = false;
int* p = nullptr;
1
2
const double pi = 3.1415926;
const char tab = '\t';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
const double pi = 3.14159;
const char newline = '\n';
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * pi * r;
cout << circle;
cout << newline;
}
31.4159
#define identifier replacement
After this directive, any occurrence of identifier
in the code is interpreted as replacement
, where replacement is any sequence of characters (until the end of the line). This replacement is performed by the preprocessor, and happens before the program is compiled, thus causing a sort of blind replacement: the validity of the types or syntax involved is not checked in any way.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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;
}
31.4159
#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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4