liveBook · Manning

Literal

As explained in Modern C, Third Edition

A literal is source text that denotes a fixed value directly in a C program.

In C, 123, 1.7E-13, 'a', and "hello" are literals: source forms that specify fixed values directly. By contrast, an identifier such as a variable name or EXIT_SUCCESS refers to an entity by name. section 2.1 section 5.3

Common literal forms include:

  • Integer literals: 123 is decimal, 077 is octal with value 63, 0xFFFF is hexadecimal, and 0b1010 is binary with value 10. The binary prefix was introduced in C23. section 5.3
  • Floating-point literals: 1.7E-13 uses a decimal exponent, while 0x1.7aP-13 uses a hexadecimal significand and a decimal exponent of 2. Hexadecimal notation can make exactly represented values easier to specify. section 5.3
  • Character literals: 'a' specifies an integer character code, while '\n' specifies a newline character. Character values are only implicitly fixed by the C standard. section 5.3
  • String literals: "hello" specifies text. As in character literals, the backslash has special meaning; for example, "first line\n" includes a newline. section 5.3

TAKEAWAY: Literal spelling affects type as well as value. Integer suffixes include U for unsigned and L for at least long; the type can also depend on the value, base, and implementation. Floating-point examples include 0.2F, 0.2, and 0.2L for float, double, and long double respectively. section 5.3

Table 5.3 Examples for literals and their types, under the supposition that signed and unsigned have the commonly used representation with 32 bits
Literal 𝑥 Value Type Value of -𝑥
2'147'483'647 +2,147,483,647 signed -2,147,483,647
2'147'483'648 +2,147,483,648 signed long -2,147,483,648
4'294'967'295 +4,294,967,295 signed long -4,294,967,295
0x7FFF'FFFF +2,147,483,647 signed -2,147,483,647
0x8000'0000 +2,147,483,648 unsigned +2,147,483,648
0xFFFF'FFFF +4,294,967,295 unsigned +1
1 +1 signed -1
1U +1 unsigned +4,294,967,295

These integer examples show how spelling and type affect the result of unary minus, assuming 32-bit signed and unsigned types. section 5.3

TAKEAWAY: Do not use string-conversion functions to determine number boundaries or scan numbers originating from literals. Accepted formats have changed across C versions, and C23 digit separators have no portable correspondence in string-processing functions. section 8.5.1

unsigned long res = strtoul("0b1", nullptr, 2);

For this conversion, C17 stops before the b and yields 0; C23 recognizes the binary prefix and yields 1. section 8.5.1

How Modern C, Third Edition covers it

How deep it goes
Explains numerical, character, and string forms, including bases, types, suffixes, and conversion portability.
Examples
Uses integer bases, decimal and hexadecimal floating-point forms, character escapes, string literals, and a strtoul conversion.
What you can do after
Evaluate/understand literal spellings and types, and recognize portability hazards when converting strings to numbers.
How it connects
Connects literals to identifiers, data types, unary negation, and string-conversion functions.

Also explained in

Compare how 6 books explain literal →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.