Literals in C and C++

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.

  • Digraph sequence - two characters enclosed in single quotation marks.
  • Trigraph sequence - three characters enclosed in single quotation marks.
  • Escape sequence - two characters enclosed in single quotation marks with the first character being a backslash character (\).

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.

Character literals in C have int type.

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:
  1. as an integer only (ie. 0.33333)
  2. as an exponential (ie. 1.15e-3 or 1.15E-3)

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.

boolean literal A boolean literal consists of the keywords true or false, and is defined as having the type bool.