Compartilhar via


C++ Character Literals

A character literal is composed of a constant character. It is represented by the character surrounded by single quotation marks. There are two kinds of character literals:

  • Narrow-character literals of type char, for example 'a'

  • Wide-character literals of type wchar_t, for example L'a'

The character used for a character literal may be any graphic character, except for reserved characters such as newline ('\n'), backslash ('\'), single quotation mark ('), and double quotation mark ("). Reserved characters are be specified with an escape sequence.

Escape Sequences

There are five kinds of escape sequences: simple, octal, hexadecimal, Unicode (UTF-8), and Unicode (UTF-16). Escape sequences may be any of the following:

Value

Escape sequence

newline

\n

horizontal tab

\t

vertical tab

\v

backspace

\b

carriage return

\r

form feed

\f

alert

\a

backslash

\\

question mark

? or \?

single quote

\'

double quote

\"

the null character

\0

octal

\ooo

hexadecimal

\xhhh

Unicode (UTF-8)

\uxxxx

Unicode (UTF-16)

\Uxxxxxxxx

The following code shows some examples of escaped characters.

#include <iostream>
using namespace std;

int main() {
    char newline = '\n';
    char tab = '\t';
    char backspace = '\b';
    char backslash = '\\';
    char nullChar = '\0';

    cout << "Newline character: " << newline << "ending" << endl; // Newline character:
                                                                  //  ending
    cout << "Tab character: " << tab << "ending" << endl; // Tab character : ending
    cout << "Backspace character: " << backspace << "ending" << endl; // Backspace character : ending
    cout << "Backslash character: " << backslash << "ending" << endl; // Backslash character : \ending
    cout << "Null character: " << nullChar << "ending" << endl; //Null character:  ending
}

For information about UTF-16 escape sequences with \U, see C++ String Literals.

Microsoft Specific

An octal escape sequence is a backslash followed by a sequence of up to 3 octal digits. The behavior of an octal escape sequence that contains more than three digits is implementation-defined; they can give surprising results. For example:

char c1 = '\100';     // char '@'
char c2 = '\1000';   // char '0' 

Escape sequences that contain non-octal characters are evaluated as the last non-octal character. For example:

char c3 = '\009'// char '9'
char c4 = '\089'     // char '9'
char c5 = '\qrs'     // char 's'

A hexadecimal escape sequence is a backslash followed by the character x, followed by a sequence of hexadecimal digits. An escape sequence that contains no hexadecimal digits causes compiler error C2153: "hex constants must have at least one hex digit". An escape sequence that has hexadecimal and non-hexadecimal characters is evaluated as the last non-hexadecimal character. The highest hexadecimal value is 0xff.

char c1 = '\x0050';  // char 'P'
char c2 = '\x0pqr';  // char 'r'

END Microsoft Specific

The backslash character (\) is a line-continuation character when it is placed at the end of a line. If you want a backslash character to appear as a character literal, you must type two backslashes in a row (\\). For more information about the line continuation character, see Phases of Translation.

Unicode Characters

Unicode characters that have a UTF-8 encoding are represented with a \u prefix. For example:

const wchar_t chr1 = L'\u79c1';

In many cases, you can just input the desired character:

const wchar_t chr2 = L'私'; 

Visual C++ Editor Settings

By default, the Visual C++ code editor uses the encoding that is appropriate for your locale. In English versions of Visual Studio the default code page is 1252. However, if you add a different kind of character, for example a Unicode character, and save the file, you see a message: "Some Unicode characters in this file could not be saved in the current codepage. Do you want to resave this file as Unicode in order to maintain your data?" You can save the file with some other encoding. You can also change the code page for a file in the File, Advanced Save Options dialog. You can turn off automatic detection of Unicode characters by unchecking “Auto-detect UTF-8 encoding without signature” in the Tools, Options, Text Editor, General options page.

See Also

Reference

C++ Literals