There also exist non-numerical constants, like:
The first two expressions represent single character constants, 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 it between double quotes
(").
When writing both single character and string literals, it is necessary to put the quotation marks surrounding them
to distinguish them from possible variable identifiers or reserved keywords. Notice the difference between these
two expressions:
x alone would refer to a variable whose identifier is x, whereas 'x'(enclosed within single quotation marks) would
refer to the character constant 'x'.
Character and string literals have certain peculiarities, like the escape codes. These are special characters that are
difficult or impossible to express otherwise in the source code of a program, like newline (\n) or tab (\t). All of
them are preceded by a backslash (\). Here you have a list of some of such escape codes:
For example:
Additionally, you can express any character by its numerical ASCII code by writing a backslash character (\)
followed by the ASCII code expressed as an octal (base-8) or hexadecimal (base-16) number. In the first case
(octal) the digits must immediately follow the backslash (for example \23or \40), in the second case
(hexadecimal), an x character must be written before the digits themselves (for example \x20or \x4A).
String literals can extend to more than a single line of code by putting a backslash sign (\) at the end of each
unfinished line.
You can also concatenate several string constants separating them by one or several blank spaces, tabulators, newline or any other valid blank character:
Finally, if we want the string literal to be explicitly made of wide characters (w char_t), instead of narrow characters
(char), we can precede the constant with the L prefix:
Wide characters are used mainly to represent non-English or exotic character sets.
Boolean literals
There are only two valid Boolean values: true and false. These can be expressed in C++ as values of type bool by
using the Boolean literals true and false.
The first two expressions represent single character constants, 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 it between double quotes
(").
When writing both single character and string literals, it is necessary to put the quotation marks surrounding them
to distinguish them from possible variable identifiers or reserved keywords. Notice the difference between these
two expressions:
x alone would refer to a variable whose identifier is x, whereas 'x'(enclosed within single quotation marks) would
refer to the character constant 'x'.
Character and string literals have certain peculiarities, like the escape codes. These are special characters that are
difficult or impossible to express otherwise in the source code of a program, like newline (\n) or tab (\t). All of
them are preceded by a backslash (\). Here you have a list of some of such escape codes:
For example:
Additionally, you can express any character by its numerical ASCII code by writing a backslash character (\)
followed by the ASCII code expressed as an octal (base-8) or hexadecimal (base-16) number. In the first case
(octal) the digits must immediately follow the backslash (for example \23or \40), in the second case
(hexadecimal), an x character must be written before the digits themselves (for example \x20or \x4A).
String literals can extend to more than a single line of code by putting a backslash sign (\) at the end of each
unfinished line.
You can also concatenate several string constants separating them by one or several blank spaces, tabulators, newline or any other valid blank character:
Finally, if we want the string literal to be explicitly made of wide characters (w char_t), instead of narrow characters
(char), we can precede the constant with the L prefix:
Wide characters are used mainly to represent non-English or exotic character sets.
Boolean literals
There are only two valid Boolean values: true and false. These can be expressed in C++ as values of type bool by
using the Boolean literals true and false.