liveBook · Manning

Literal

As explained in Modern C

A literal is a C source notation that directly represents a fixed value.

In `printf("element %zu is %g\n", i, 9.0)`, the quoted text and `9.0` represent fixed values written directly in the program. They are literals. The names `printf` and `i` are identifiers, not literals. section 2.1

A literal’s spelling helps determine what value it specifies:

  • `123` is a decimal integer literal; `077` is octal and has value 63; `0xFFFF` is hexadecimal. section 5.3
  • `1.7E-13` is a decimal floating-point literal. `0x1.7aP-13` is a hexadecimal floating-point form. section 5.3
  • `"first line\n"` is a string literal: text known at compile time. Consecutive string literals are concatenated, so a long string can be split across source lines. section 5.3

**TAKEAWAY:** Numerical literals are never negative. In `-34`, the minus sign is a negation operator applied to the literal `34`; in `-1.5E-23`, the exponent’s minus sign is part of the floating-point literal. Every literal also has a type, which is a separate question from whether its value is positive. section 5.3

A decimal integer literal takes the first signed integer type that fits its value. For example, if the largest `signed` value is 32767, `32768` requires `signed long`; `-32768` therefore also has type `signed long`. Do not infer a literal’s type from the apparent type of a value after negation. section 5.3

Table 5.3. Examples for constants and their types. This is under the supposition that signed and unsigned have the commonly used representation with 32 bits (view table figure)

Constant x

Value

Type

Value of –x

2147483647 +2147483647 signed –2147483647
2147483648 +2147483648 signed long –2147483648
4294967295 +4294967295 signed long –4294967295
0x7FFFFFFF +2147483647 signed –2147483647
0x80000000 +2147483648 unsigned +2147483648
0xFFFFFFFF +4294967295 unsigned +1
1 +1 signed –1
1U +1 unsigned +4294967295

Compare the listed types and negated results for decimal, hexadecimal, and `U`-suffixed constants. The table’s results depend on its stated 32-bit representation assumption. section 5.3

Integer literals also appear in preprocessor conditions used to reject an unsuitable execution platform during compilation. Such conditions evaluate macros and integer literals; an unknown identifier evaluates to 0. section 8.1.5

How Modern C covers it

How deep it goes
Defines literals, compares numeric spellings, and works through sign and integer-type rules without developing all type-selection cases.
Examples
Uses decimal, octal, and hexadecimal constants, adjacent strings passed to `puts`, and a table comparing integer types and negated values.
What it assumes
Introduces literals alongside basic C grammar, then relies on the distinction between a value and its type for numerical rules.
What you can do after
Evaluate/understand literal spellings, distinguish negation from a numerical literal, and determine a decimal integer literal’s first fitting signed type.
How it connects
Connects literals to identifiers, integer types, negation, string concatenation, and preprocessor conditions.

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. The section links point to the parts of the book it draws on.