There are two types of character constants — integral
character constants and wide character constants.
Integral character constants are of type int.
They do not have type char.
However, because a char
is normally converted to an int
in an expression, this seldom is a problem. The contents can be
ASCII characters, octal escape sequences, or hexadecimal escape
sequences.
Octal escape sequences consist of a backslash, ( \
) followed by up to three octal digits. Hexadecimal escape sequences
also start with a backslash, which is followed by lowercase x and
any number of hexadecimal digits. It is terminated by any non-hexadecimal
characters.
The digits of the escape sequences are converted into a single
8-bit character and stored in the character constant at that point.
For example, the following character constants have the same value:
They all represent the decimal value 65.
Character constants are not restricted to one character; multi-character
constants are allowed. The value of an integral character constant
containing more than one character is computed by concatenating
the 8-bit ASCII code values of the characters, with the leftmost
character being the most significant. For example, the character
constant 'AB'
has the value 256*'A'+'B' = 256*65+66 = 16706.
Only the rightmost four characters participate in the computation.
Wide character constants (type wchar_t)
are of type unsigned int.
A wide character constant is a sequence of one or more multibyte
characters enclosed in single quotes and prefixed by the letter
L. The value
of a wide character constant containing a single multibyte character
is a member of the extended execution character set whose value
corresponds to that of the multibyte character. The value of a multibyte
character can be found by calling the function mbtowc.
For multi-character wide character constants, the entire content
of the constant is extracted into an unsigned integer and the resulting
character is represented by the final value.
Some characters are given special representation in escape
sequences. These are nonprinting and special characters that programmers
often need to use (listed in Table 2-1 “Special Characters ”).
Table 2-1 Special Characters
Character | Description |
---|
\n | New line |
\t | Horizontal tab |
\v | Vertical tab |
\b | Backspace |
\r | Carriage return |
\f | Form feed |
\\ | Backslash character |
\' | Single quote |
\' | Double quote |
\a | Audible or visible alert (control G) |
\? | Question mark character '?' |