Previous Table of Contents Next


3.2.5 Literals


   This section describes the following literals:

   3.2.5.1 Integer Literals

   An integer literal consisting of a sequence of digits is taken to be decimal (base ten) unless it begins with 0 (digit zero). A sequence of digits starting with 0 is taken to be an octal integer (base eight). The digits 8 and 9 are not octal digits. A sequence of digits preceded by 0x or 0X is taken to be a hexadecimal integer (base sixteen). The hexadecimal digits include a or A through f or F with decimal values ten through fifteen, respectively. For example, the number twelve can be written 12, 014, or 0XC.

   3.2.5.2 Character Literals

   A character literal is one or more characters enclosed in single quotes, as in ’x.’ Character literals have type char.

   A character is an 8-bit quantity with a numerical value between 0 and 255 (decimal). The value of a space, alphabetic, digit, or graphic character literal is the numerical value of the character as defined in the ISO Latin-1 (8859.1) character set standard (See Table 3-2 on page 3-3, Table 3-3 on page 3-4, and Table 3-4 on page 3-4). The value of a null is 0. The value of a formatting character literal is the numerical value of the character as defined in the ISO 646 standard (see Table 3-5 on page 3-5). The meaning of all other characters is implementation-dependent.

   Nongraphic characters must be represented using escape sequences as defined below in Table 3-9. Note that escape sequences must be used to represent single quote and backslash characters in character literals.

   Table 3-9 Escape Sequences Table 3-9 Escape Sequences (Continued)

Description

Escape Sequence

newline \n
horizontal tab \t
vertical tab \v
backspace \b
carriage return \r
form feed \f
alert \a
backslash \\
question mark \?
single quote \'
double quote \"
octal number \ooo

Description

Escape Sequence

hexadecimal number \xhh
unicode character \uhhhh

   If the character following a backslash is not one of those specified, the behavior is undefined. An escape sequence specifies a single character.

   The escape \ooo consists of the backslash followed by one, two, or three octal digits that are taken to specify the value of the desired character. The escape \xhh consists of the backslash followed by x followed by one or two hexadecimal digits that are taken to specify the value of the desired character.

   The escape \uhhhh consists of a backslash followed by the character ‘u’, followed by one, two, three or four hexadecimal digits. This represents a unicode character literal. Thus the literal “\u002E? represents the unicode period ‘.’ character and the literal “\u3BC? represents the unicode greek small letter ‘mu’. The \u escape is valid only with wchar and wstring types. Because a wide string literal is defined as a sequence of wide character literals a sequence of \u literals can be used to define a wide string literal. Attempts to set a char type to a \u defined literal or a string type to a sequence of \u literals result in an error.

   A sequence of octal or hexadecimal digits is terminated by the first character that is not an octal digit or a hexadecimal digit, respectively. The value of a character constant is implementation dependent if it exceeds that of the largest char.

   Wide character literals have an L prefix, for example:

   const wchar C1 = L'X';

   Attempts to assign a wide character literal to a non-wide character constant or to assign a non-wide character literal to a wide character constant result in a compile-time diagnostic.

   Both wide and non-wide character literals must be specified using characters from the ISO 8859-1 character set.

   3.2.5.3 Floating-point Literals

   A floating-point literal consists of an integer part, a decimal point, a fraction part, an e or E, and an optionally signed integer exponent. The integer and fraction parts both consist of a sequence of decimal (base ten) digits. Either the integer part or the fraction part (but not both) may be missing; either the decimal point or the letter e (or E) and the exponent (but not both) may be missing.

   3.2.5.4 String Literals

    A string literal is a sequence of characters (as defined in Section 3.2.5.2, “Character Literals,? on page 3-9), with the exception of the character with numeric value 0, surrounded by double quotes, as in “...?.

   Adjacent string literals are concatenated. Characters in concatenated strings are kept distinct. For example,

    "\xA" "B"

   contains the two characters '\xA' and 'B' after concatenation (and not the single hexadecimal character '\xAB').

   The size of a string literal is the number of character literals enclosed by the quotes, after concatenation. Within a string, the double quote character " must be preceded by a \.

   A string literal may not contain the character ‘\0’.

   Wide string literals have an L prefix, for example:

   const wstring S1 = L"Hello";

   Attempts to assign a wide string literal to a non-wide string constant or to assign a non-wide string literal to a wide string constant result in a compile-time diagnostic.

   Both wide and non-wide string literals must be specified using characters from the ISO 8859-1 character set.

   A wide string literal shall not contain the wide character with value zero.

   3.2.5.5 Fixed-Point Literals

   A fixed-point decimal literal consists of an integer part, a decimal point, a fraction part and a d or D. The integer and fraction parts both consist of a sequence of decimal (base 10) digits. Either the integer part or the fraction part (but not both) may be missing; the decimal point (but not the letter d (or D)) may be missing.