C Language Character literals


Example

Character literals are a special type of integer literals that are used to represent one character. They are enclosed in single quotes, e.g. 'a' and have the type int. The value of the literal is an integer value according to the machine's character set. They do not allow suffixes.

The L prefix before a character literal makes it a wide character of type wchar_t. Likewise since C11 u and U prefixes make it wide characters of type char16_t and char32_t, respectively.

When intending to represent certain special characters, such as a character that is non-printing, escape sequences are used. Escape sequences use a sequence of characters that are translated into another character. All escape sequences consist of two or more characters, the first of which is a backslash \. The characters immediately following the backslash determine what character literal the sequence is interpreted as.

Escape SequenceRepresented Character
\bBackspace
\fForm feed
\nLine feed (new line)
\rCarriage return
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\nnnOctal value
\xnn...Hexadecimal value
C89
Escape SequenceRepresented Character
\aAlert (beep, bell)
C99
Escape SequenceRepresented Character
\unnnnUniversal character name
\UnnnnnnnnUniversal character name

A universal character name is a Unicode code point. A universal character name may map to more than one character. The digits n are interpreted as hexadecimal digits. Depending on the UTF encoding in use, a universal character name sequence may result in a code point that consists of multiple characters, instead of a single normal char character.

When using the line feed escape sequence in text mode I/O, it is converted to the OS-specific newline byte or byte sequence.

The question mark escape sequence is used to avoid trigraphs. For example, ??/ is compiled as the trigraph representing a backslash character '\', but using ?\?/ would result in the string "??/".

There may be one, two or three octal numerals n in the octal value escape sequence.