A literal is a sequence of characters. There are several types of literals in C and C++. The following table contains a short description of types of literals.
| Literal_Type | Description |
|---|---|
| integer literal | The type of characters in an integer literal are
numeric. In the case of representing octal and
hexadecimal values, the character type is alphanumeric.
Integer literals have the type int. Variations of the
integer literal exist and are described in data types. Example: temperature = -25; NumberOfToes = 10; SecretCode = 0xFFFFFF; |
| character literal | A character literal is denoted by a character
enclosed in single quotation marks and has the type char.
All characters in the valid character set qualify as
character literals. A single byte of storage is reserved
for ordinary character literals. There are some exceptions to the definition of a character literal. The following sequences can also be defined as character literals.
A special character literal with an L preceeding the character, is known as a wide character literal and has type wchar_t. For special characters that require more than a single byte of storage, the wide character literal or wchar_t type is used. The wchar_t is capable of storing characters from the valid character set and the double byte character set.
|
| floating point literal | A floating point literal consists of an integer
prefix, an optional decimal point, followed by an
optional fraction suffix. The fraction suffix can be
denoted in two ways:
Variations of a floating point literal can be distinguished by a character suffix. Floating point literals with an f or F suffix have type float, while floating literals with a l or L suffix have type long double. |
| string literal | A string literal is a composed of a series of
characters enclosed in double quotation marks. Similar to
the character literal, the string literal can also be
prefixed by the letter L like so L"string".
Without the L prefix, the string literal is known as a
narrow string literal. The addition of the L prefix
characterizes the literal as a wide string literal. The definition of a narrow string literal is an array of type const char of length n. The definition of a wide string literal is an array of type const wchar_t of length n. |
| A boolean literal consists of the keywords true or false, and is defined as having the type bool. |
