Character literals
Character literals are enclosed in single quotes ‘’ and can contain the same escape sequences as strings - with one exception: the platform dependent newline (\p) is not allowed as it may be wider than one character (often it is the pair CR/LF for example). Here are the valid escape sequences for character literals:
Escape sequence | Meaning |
---|---|
\r, \c | carriage return |
\n, \l | line feed |
\f | form feed |
\t | tabulator |
\v | vertical tabulator |
\ | backslash |
\” | quotation mark |
\’ | apostrophe |
\ ‘0’..’9’+ | character with decimal value d; all decimal digits directly following are used for the character |
\a | alert |
\b | backspace |
\e | escape [ESC] |
\x HH | character with hex value HH; exactly two hex digits are allowed |
A character is not an Unicode character but a single byte. The reason for this is efficiency: for the overwhelming majority of use-cases, the resulting programs will still handle UTF-8 properly as UTF-8 was specially designed for this. Another reason is that Nim can thus support array[char, int] or set[char] efficiently as many algorithms rely on this feature. The Rune type is used for Unicode characters, it can represent any Unicode character. Rune is declared in the unicode module.