1
a = 5;
5 in this piece of code was a literal constant.1
2
3
1776
707
-
1776 always represents the value one thousand seven hundred seventy-six.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
int. However, certain suffixes may be appended to an integer literal to specify a different integer type:| Suffix | Type modifier |
|---|---|
u or U | unsigned |
l or L | long |
ll or LL | long long |
unsigned long or unsigned long long.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
double. Floating-point literals of type float or long double can be specified by adding one of the following suffixes:| Suffix | Type |
|---|---|
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 (").
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).\n) or tab (\t). These special characters are all of them preceded by a backslash character (\).| 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).1
2
"this forms" "a single" " string "
"of characters"
1
"this formsa single string of characters"
\) 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.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 |
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 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.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.