Character Constants [ HP C/iX Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Reference Manual
Character Constants
A character constant is a constant that is enclosed in single quotes.
Syntax
character-constant:
'c-char-sequence'
L'c-char-sequence'
c-char-sequence:
c-char
c-char-sequence c-char
c-char:
any character in the source character set except
the single-quote ', backslash \, or new-line character
escape-sequence
escape-sequence:
simple-escape-sequence
octal-escape-sequence
hexadecimal-escape-sequence
simple-escape-sequence: one of
\' \" \? \\
\a \b \f \n \r \t \v
octal-escape-sequence:
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence:
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
NOTE \a and \? are available only in ANSI mode.
Description
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:
'A' '\101' '\x41'
They all represent the decimal value 65.
Character constants are not restricted to one character; multi-character
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 below).
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 '?' |
| | |
-------------------------------------------------------
Examples
'a' represents the letter a, the value 97
'\n' represents the newline character, the value 10
'\?' represents a question mark, the value 63
'7' represents the character 7, the value 55
'\0' represents the null character, the value 0
'\101' represents the letter A, the value 65
MPE/iX 5.0 Documentation