liveBook · Manning

Literal

As explained in Modern C

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

In C, 3, .00007, and "element" are literals: source notations that directly specify fixed values. Numerical literals specify numbers; string literals specify text known at compile time. Every literal has both a value and a type. section 2.1 section 5.3

Numerical literals can use several forms:

  • 123 is a decimal integer constant. A leading 0 selects octal: 077 has the value 63. section 5.3
  • 0xFFFF is a hexadecimal integer constant; the x and hexadecimal letters may also be uppercase. section 5.3
  • 1.7E-13 is a decimal floating-point constant in scientific notation. Hexadecimal floating-point notation is also available, as in 0x1.7aP-13. section 5.3

TAKEAWAY: Numerical literals are never negative. In -34, the leading minus is a negation operator applied to the literal 34. By contrast, the minus in the exponent of 1.5E-23 is part of the floating-point literal. section 5.3

A sign can affect the expression’s type

Suppose signed has a maximum value of 32767 on a platform. The decimal constant 32768 does not fit in signed, so it has type signed long; consequently, -32768 also has type signed long. Decimal integer constants use the first of three signed types that fits their value. section 5.3

TAKEAWAY: Consecutive string literals are concatenated. Splitting a longer piece of text into adjacent quoted chunks can make it easier to include in source code. section 5.3

puts("first line\n"
     "another line\n"
     "first and "
     "second part of the third line");


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}]]"}
!@%STYLE%@!

This puts call splits text across consecutive string literals, including two chunks that form one line. section 5.3

Integer literals also have a role in preprocessor conditions. Use macros and integer literals when evaluating such conditions; unknown identifiers evaluate to 0. section 8.1.5

How Modern C covers it

How deep it goes
Explains literal notation, string concatenation, sign handling, and decimal integer type selection, including a platform-dependent boundary example.
Examples
Uses octal 077, scientific notation, adjacent string literals, and -32768 on a platform with a 32767 signed maximum.
What you can do after
Evaluate literal forms and their types, including why a leading minus is an operator rather than part of a numerical literal.
How it connects
Connects literals to 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.