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 `double x = 9.0;`, `9.0` is a literal: source text that specifies a fixed value. Literals also include integers such as `0` and `3`, character literals such as `'a'`, and string literals such as `"hello"`. Unlike an identifier, a literal spells out a value rather than naming a program entity. section 2.1 section 5.3

A literal’s spelling determines how its value is read:

  • `123` is decimal. A leading `0` makes `077` octal, with value 63. `0xFFFF` is hexadecimal. The binary form `0b1010`, with value 10, was introduced in C23. section 5.3
  • `1.7E-13` uses a decimal exponent: `mEe` means `m × 10^e`. `0x1.7aP-13` uses a hexadecimal fraction and a power of two; its exponent is written in decimal. section 5.3
  • `'a'` is an integer character literal representing a character code. In `'\n'`, the backslash introduces a special character. A string literal such as `"hello"` specifies text, also with special backslash notation. section 5.3

TAKEAWAY: A numeral’s value does not determine its type by itself. Spelling and suffixes matter: `1` is signed in the illustrated case, while `1U` is unsigned. Integer suffixes include `L`, `LL`, and `U`; floating-point examples include `0.2F`, `0.2`, and `0.2L`. String literals are the exception to the rule that literals specify numbers. 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

Compare equal-valued decimal and hexadecimal literals under the table’s stated 32-bit representation assumption. Notice that `0x8000'0000` is unsigned there, so applying `-` does not produce a negative value. section 5.3

Literal syntax is not string-conversion syntax

`strtoul("0b1", nullptr, 2)` encounters different accepted formats across standard versions: the result is 0 under C17 and 1 under C23. The call processes characters in a string literal; it does not ask the compiler to interpret `0b1` as a numeric literal. Likewise, C23 digit separators in number literals have no portable counterpart in these conversion functions. TAKEAWAY: Do not use string-conversion functions to find the boundaries of, or scan text originating from, number literals. section 8.5.1

How Modern C, Third Edition covers it

How deep it goes
Works through numeric bases, exponent forms, character and string notation, and type-dependent examples; does not develop literal parsing internals.
Examples
Compares `077` with 63, `0b1010` with 10, and `strtoul("0b1", nullptr, 2)` across C17 and C23.
What it assumes
Starts with literals among the tokens of a C program, then defines their numeric forms and types.
What you can do after
Evaluate/understand it by reading common C literal forms and recognizing when type or string-conversion rules change their interpretation.
How it connects
Connects literal spelling to integer types, floating-point types, character codes, string literals, and string-to-number conversion.

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.