Character constants

suggest change

An ordinary character constant is enclosed in single quotation marks and has type int:

int letter = 'A';
int newline = '\n';

Its value is determined by the execution character set. Do not assume that letters are encoded as ASCII unless the target environment guarantees it.

Common escape sequences include:

Escape Meaning
\a alert
\b backspace
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\" double quote
\nnn one to three octal digits
\xhh... one or more hexadecimal digits

A hexadecimal escape consumes every following hexadecimal digit, so use adjacent literals or another separator when the next source character could be consumed accidentally.

Universal character names use exactly four hexadecimal digits after \u or eight after \U. They identify Unicode scalar values subject to the restrictions of the selected C version; conversion to the execution encoding still depends on the literal's prefix and the implementation.

The prefixes L, u, and U produce wide character constants associated with wchar_t, char16_t, and char32_t. C23 also supports u8 character constants associated with char8_t.

A constant containing more than one ordinary character, such as 'ab', is an implementation-defined multicharacter constant. Avoid it in portable code.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents