The term literal constant, or literal, refers to a value that occurs in a program and cannot be changed. The C language uses the term constant in place of the noun literal. The adjective literal adds to the concept of a constant the notion that we can speak of it only in terms of its value. A literal constant is nonaddressable, which means that its value is stored somewhere in memory, but we have no means of accessing that address.
Every literal has a value and a data type. The value of any literal does not change while the program runs and must be in the range of representable values for its type. The following are the available types of literals:
Integer literals are numbers that do not have a decimal point or an exponential part. They can be represented as:
An integer literal may have a prefix that specifies its base, or a suffix that specifies its type.
Integer literal syntax >>-+-decimal_constant-----+--+---------------+----------------->< +-octal_constant-------+ +-+-l--+--+---+-+ '-hexadecimal_constant-' | +-L--+ +-u-+ | | +-ll-+ '-U-' | | '-LL-' | '-+-u-+--+----+-' '-U-' +-l--+ +-L--+ +-ll-+ '-LL-'
The data type of an integer literal is determined by its form, value, and suffix. The following table lists the integer literals and shows the possible data types. The smallest data type that can represent the constant value is used to store the constant.
| Integer literal | Possible data types |
|---|---|
| unsuffixed decimal | int, long int, long long int |
| unsuffixed octal or hexadecimal | int, unsigned int, long int, unsigned long int, long long int, unsigned long long int |
| decimal, octal, or hexadecimal suffixed by u or U | unsigned int, unsigned long int, unsigned long long int |
| decimal suffixed by l or L | long int, long long int |
| octal or hexadecimal suffixed by l or L | long int, unsigned long int, long long int, unsigned long long int |
| decimal, octal, or hexadecimal suffixed by both u or U, and l or L | unsigned long int, unsigned long long int |
| decimal suffixed by ll or LL | long long int |
| octal or hexadecimal suffixed by ll or LL | long long int, unsigned long long int |
| decimal, octal, or hexadecimal suffixed by both u or U, and ll or LL | unsigned long long int |
Related information
A decimal integer literal contains any of the digits 0 through 9. The first digit cannot be 0. Integer literals beginning with the digit 0 are interpreted as an octal integer literal rather than as a decimal integer literal.
Decimal integer literal syntax .--------------. V | >>-digit_1_to_9----digit_0_to_9-+------------------------------><
A plus (+) or minus (-) symbol can precede a decimal integer literal. The operator is treated as a unary operator rather than as part of the literal.
The following are examples of decimal literals:
485976 -433132211 +20 5
A hexadecimal integer literal begins with the 0 digit followed by either an x or X, followed by any combination of the digits 0 through 9 and the letters a through f or A through F. The letters A (or a) through F (or f) represent the values 10 through 15, respectively.
Hexadecimal integer literal syntax .------------------. V | >>-+-0x-+----+-digit_0_to_f-+-+-------------------------------->< '-0X-' '-digit_0_to_F-'
The following are examples of hexadecimal integer literals:
0x3b24 0XF96 0x21 0x3AA 0X29b 0X4bD
An octal integer literal begins with the digit 0 and contains any of the digits 0 through 7.
Octal integer literal syntax .--------------. V | >>-0----digit_0_to_7-+-----------------------------------------><
The following are examples of octal integer literals:
0 0125 034673 03245
There are only two Boolean literals: true and false.
Related information
Floating-point literals are numbers that have a decimal point or an exponential part. They can be represented as:
A real binary floating-point constant consists of the following:
Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.
Binary floating-point literal syntax .-----------. .-------. V | V | >>-+---+-------+-+--.----digit-+--+--------------+-+--+---+---->< | '-digit-' '-| exponent |-' | +-f-+ | .-------. | +-F-+ | V | | +-l-+ +---digit-+--.--+--------------+----------------+ '-L-' | '-| exponent |-' | | .-------. | | V | | '---digit-+--| exponent |-----------------------' Exponent: .-------. V | |--+-e-+--+----+----digit-+-------------------------------------| '-E-' +-+--+ '- --'
The suffix f or F indicates a type of float, and the suffix l or L indicates a type of long double. If a suffix is not specified, the floating-point constant has a type double.
A plus (+) or minus (-) symbol can precede a floating-point literal. However, it is not part of the literal; it is interpreted as a unary operator.
The following are examples of floating-point literals:
| Floating-point constant | Value |
|---|---|
| 5.3876e4 | 53,876 |
| 4e-11 | 0.00000000004 |
| 1e+5 | 100000 |
| 7.321E-3 | 0.007321 |
| 3.2E+4 | 32000 |
| 0.5e-6 | 0.0000005 |
| 0.45 | 0.45 |
| 6.e10 | 60000000000 |
Related information
Real hexadecimal floating constants, which are a C99 feature, consist of the following:
The significant part represents a rational number and is composed of the following:
The optional fraction part is a period followed by a sequence of hexadecimal digits.
The exponent part indicates the power of 2 to which the significant part is raised, and is an optionally signed decimal integer. The type suffix is optional. The full syntax is as follows:
Hexadecimal floating-point literal syntax >>-+-0x-+-------------------------------------------------------> '-0X-' .------------------. .------------------. V | V | >--+---+--------------+-+--.----+-digit_0_to_f-+-+--| exponent |-+--> | +-digit_0_to_f-+ '-digit_0_to_F-' | | '-digit_0_to_F-' | | .------------------. | | V | | +---+-digit_0_to_f-+-+--.--| exponent |-----------------------+ | '-digit_0_to_F-' | | .------------------. | | V | | '---+-digit_0_to_f-+-+--| exponent |--------------------------' '-digit_0_to_F-' >--+---+------------------------------------------------------->< +-f-+ +-F-+ +-l-+ '-L-' Exponent: .--------------. V | |--+-p-+--+----+----digit_0_to_9-+------------------------------| '-P-' +-+--+ '- --'
The suffix f or F indicates a type of float, and the suffix l or L indicates a type of long double. If a suffix is not specified, the floating-point constant has a type double. You can omit either the whole-number part or the fraction part, but not both. The binary exponent part is required to avoid the ambiguity of the type suffix F being mistaken for a hexadecimal digit.
A real decimal floating-point constant consists of the following:
Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.
Decimal floating-point literal syntax .-----------. .-------. V | V | >>-+---+-------+-+--.----digit-+--+--------------+-+--+-df-+--->< | '-digit-' '-| exponent |-' | +-DF-+ | .-------. | +-dd-+ | V | | +-DD-+ +---digit-+--.--+--------------+----------------+ +-dl-+ | '-| exponent |-' | '-DL-' | .-------. | | V | | '---digit-+--| exponent |-----------------------' Exponent: .-------. V | |--+-e-+--+----+----digit-+-------------------------------------| '-E-' +-+--+ '- --'
The suffix df or DF indicates a type of _Decimal32, the suffix dd or DD indicates a type of _Decimal64, and the suffix dl or DL indicates a type of _Decimal128. If a suffix is not specified, the floating-point constant has a type double.
You cannot mix cases in the literal suffix.
The following are examples of decimal floating-point literal declarations:
_Decimal32 a = 22.2df; _Decimal64 b = 33.3dd;
When using decimal floating-point data, more accurate results will occur if decimal floating-point literals are used. As previously stated, an unsuffixed floating-point constant has type double. Consider this initialization:
_Decimal64 rate = 0.1;
The constant 0.1 has type double, which cannot accurately represent the decimal value 0.1. The variable rate will get a value slightly different from 0.1. The definition should be coded as follows:
_Decimal64 rate = 0.1dd;
When the decimal floating-point literals are converted or when the constant decimal floating-point expressions are resolved, the default rounding mode used will be "round to the nearest, ties to even."
A packed decimal literal is a kind
of floating-point literal that provides the ability to accurately
represent large numeric quantities. It can consist of an integral
part, a decimal point, a fractional part, and the mandatory suffix D.
A packed decimal literal can have up to sixty-three significant
digits, including integral and fractional parts.
A packed decimal literal is of the form:
.-----------. .-------. V | V | >>-----+-------+-+--.----digit-+----+-d-+---------------------->< '-digit-' '-D-'
Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both.
Related References
A character literal contains a sequence of characters or escape sequences enclosed in single quotation mark symbols, for example 'c'. A character literal may be prefixed with the letter L, for example L'c'. A character literal without the L prefix is an ordinary character literal or a narrow character literal. A character literal with the L prefix is a wide character literal. An ordinary character literal that contains more than one character or escape sequence (excluding single quotes ('), backslashes (\) or new-line characters) is a multicharacter literal.
The type of a narrow character literal
is int. The type of a wide character literal is wchar_t.
The type of a multicharacter literal is int.
The type of a character literal that
contains only one character is char, which is an integral
type. The type of a wide character literal is wchar_t.
The type of a multicharacter literal is int.
Character literal syntax .---------------------. V | >>-+---+--'----+-character-------+-+--'------------------------>< '-L-' '-escape_sequence-'
At least one character or escape sequence must appear in the character literal, and the character literal must appear on a single logical source line.
The characters can be from the source program character set. You can represent the double quotation mark symbol by itself, but to represent the single quotation mark symbol, you must use the backslash symbol followed by a single quotation mark symbol ( \' escape sequence). (See Escape sequences for a list of other characters that are represented by escape characters.)
The value of a narrow or wide character literal containing
a single character is the numeric representation of the character
in the character set used at run time. The lowest four bytes represent
the value of an integer character literal that contains more than
one character. The lowest two bytes of the lowest multibyte character
represent the value of a wide character literal. For the locale type utf
LOCALETYPE(*LOCALEUTF), the lowest four bytes of the lowest
multibyte character represent the value of the wide character literal.
Outside of the basic source character set, the universal character names for letters and digits are allowed in C++ .
The following are examples of character literals:
'a'
'\''
L'0'
'('
Related information
A string literal contains a sequence of characters or escape sequences enclosed in double quotation mark symbols. A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal.
The type of a narrow
string literal is array of char. The type of a wide string
literal is array of wchar_t.
The type of a narrow string literal
is array of const char. The type of a wide string literal
is array of const wchar_t. Both types have static storage
duration.
A null ('\0') character is appended to each string. For a wide string literal, the value '\0' of type wchar_t is appended. By convention, programs recognize the end of a string by finding the null character.
String literal syntax .---------------------. V | >>-+---+--"----+-character-------+-+--"------------------------>< '-L-' '-escape_sequence-'
Multiple spaces contained within a string literal are retained.
Use the escape sequence \n to represent a new-line character as part of the string. Use the escape sequence \\ to represent a backslash character as part of the string. You can represent a single quotation mark symbol either by itself or with the escape sequence \'. You must use the escape sequence \" to represent a double quotation mark.
Outside of the basic source character set, the universal character names for letters and digits are allowed in C++.
The following are examples of string literals:
char titles[ ] = "Handel's \"Water Music\""; char *temp_string = "abc" "def" "ghi"; /* *temp_string = "abcdefghi\0" */ wchar_t *wide_string = L"longstring";
This example illustrates escape sequences in string literals:
#include <iostream>
using namespace std;
int main () {
char *s ="Hi there! \n";
cout << s;
char *p = "The backslash character \\.";
cout << p << endl;
char *q = "The double quotation mark \".\n";
cout << q ;
} This program produces the following output:
Hi there! The backslash character \. The double quotation mark ".
To continue a string on the next line, use the line continuation character (\ symbol) followed by optional whitespace and a new-line character (required). For example:
char *mail_addr = "Last Name First Name MI Street Address \
893 City Province Postal code ";
In the following example, the string literal second causes a compile-time error.
char *first = "This string continues onto the next\
line, where it ends."; /* compiles successfully. */
char *second = "The comment makes the \ /* continuation symbol
*/ invisible to the compiler."; /* compilation error. */
Related information
Another way to continue a string is to have two or more consecutive strings. Adjacent string literals will be concatenated to produce a single string. For example:
"hello " "there" /* is equivalent to "hello there" */ "hello" "there" /* is equivalent to "hellothere" */
Characters in concatenated strings remain distinct. For example, the strings "\xab" and "3" are concatenated to form "\xab3". However, the characters \xab and 3 remain distinct and are not merged to form the hexadecimal character \xab3 .
The concatenation of a wide string literal and narrow string literal is not supported in ILE C/C++. If a wide string literal and a narrow string literal are adjacent, as in the following:
"hello " L"there"
the compiler will issue an error message.
Following any concatenation, '\0' of type char is appended at the end of each string. For a wide string literal, '\0' of type wchar_t is appended. C++ programs find the end of a string by scanning for this value. For example:
char *first = "Hello "; /* stored as "Hello \0" */ char *second = "there"; /* stored as "there\0" */ char *third = "Hello " "there"; /* stored as "Hello there\0" */