Character Types
Character types store human language data. The six character types are:
char
The default type, always 1 byte. May or may not be signed.
(Example: ASCII.)
Types
37
char16_t
Used for 2-byte character sets. (Example: UTF-16.)
char32_t
Used for 4-byte character sets. (Example: UTF-32.)
signed char
Same as
char
but guaranteed to be signed.
unsigned char
Same as
char
but guaranteed to be unsigned.
wchar_t
Large enough to contain the largest character of the imple-
mentation’s locale. (Example: Unicode.)
The character types
char
,
signed char
, and
unsigned char
are called narrow
characters, whereas
char16_t
,
char32_t
, and
wchar_t
are called wide characters due
to their relative storage requirements.
Character Literals
A character literal is a single, constant character. Single quotation marks (
' '
)
surround all characters. If the character is any type but
char
, you must also
provide a prefix:
L
for
wchar_t
,
u
for
char16_t
, and
U
for
char32_t
. For example,
'J'
declares a
char
literal and
L'J'
declares a
wchar_t
.
Escape Sequences
Some characters don’t display on the screen. Instead, they force the display
to do things like move the cursor to the left side of the screen (carriage
return) or move the cursor down one line (newline). Other characters can
display onscreen, but they’re part of the C++ language syntax, such as single
or double quotes, so you must use them very carefully. To put these charac-
ters into a
char
, you use the escape sequences, as listed in Table 2-3.
Table 2-3:
Reserved Characters and Their Escape Sequences
Value
Escape sequence
Newline
\n
Tab (horizontal)
\t
Tab (vertical)
\v
Backspace
\b
Carriage return
\r
Form feed
\f
Alert
\a
Backslash
\\
Question mark
?
or
\?
Single quote
\'
Double quote
\"
The null character
\0
|