C Programming Character Literals

Last modified by Microchip on 2023/11/10 10:59

Rules

  • Must be surrounded by single quotes (').
  • May include any single printable character (e.g. 'a').
  • May include any single non-printable character using escape sequences, also called diagraphs (e.g. '\0' for NUL).

Examples of Valid Character Literals

'a''T''\n''5''@'' ' (space)

Examples of Invalid Character Literals

'me''23''''

Because the single quote (') is used to delimit character literals, the single quote cannot by itself be a character literal. It must be specified using an escape sequence: '\''.

Information

Although escape sequences look like two characters, the backslash (\) is treated specially in character and string literals. It signals that the character that follows is special. It either represents a non-printable character, as in the case of '\0' for NUL, or a character that cannot be used on its own in a character literal, such as the single quote or backslash themselves, which are represented as '\'' and '\\' respectively.