liveBook · Manning

Constant

As explained in Modern C

A named constant is a constant associated with an explanatory name representing a particular meaning.

A repeated value such as 3 can carry different meanings in one program. Naming each meaning makes the code easier to maintain and prevents accidental identification of equal values. section 5.6

char const*const bird[3] = {
  "raven",
  "magpie",
  "jay",
};
char const*const pronoun[3] = {
  "we",
  "you",
  "they",
};
char const*const ordinal[3] = {
  "first",
  "second",
  "third",
};
...
for (unsigned i = 0; i < 3; ++i)
    printf("Corvid %u is the %s\n", i, bird[i]);
...
for (unsigned i = 0; i < 3; ++i)
    printf("%s plural pronoun is %s\n", ordinal[i], pronoun[i]);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":5},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":5},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":5},{\"line\":10,\"ch\":10}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":3}],[{\"line\":16,\"ch\":5},{\"line\":16,\"ch\":13}],[{\"line\":19,\"ch\":5},{\"line\":19,\"ch\":13}],[{\"line\":17,\"ch\":4},{\"line\":17,\"ch\":10}],[{\"line\":20,\"ch\":4},{\"line\":20,\"ch\":10}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":3}],[{\"line\":16,\"ch\":5},{\"line\":16,\"ch\":13}],[{\"line\":19,\"ch\":5},{\"line\":19,\"ch\":13}],[{\"line\":17,\"ch\":4},{\"line\":17,\"ch\":10}],[{\"line\":20,\"ch\":4},{\"line\":20,\"ch\":10}]]"}
!@%STYLE%@!

The listing places explanatory names such as bird, pronoun, and ordinal beside related string values, then uses those names in output. section 5.6

TAKEAWAY 1: Constants with a particular meaning must be named. Constants that happen to be equal but mean different things must remain distinguishable. section 5.6

C supplies several ways to approximate or provide named constants:

  • Enumeration constants provide C's proper named-constant mechanism. section 5.6
  • Macros provide generic text replacement and produce compile-time constants only when replacements use literals of base types. section 5.6
  • Temporary objects provide a constant-like approach for more-complicated data types. section 5.6

Name the artifact, not merely the value

Names such as corvid_num, M_PI, false, true, SIZE_MAX, and fortytwo identify constants that stand out because of factual, standard-defined, platform-specific, external, cultural, or design significance. section 9.2

How Modern C covers it

How deep it goes
Defines naming principles and surveys enumeration constants, macros, and temporary objects without detailing their syntax.
Examples
Uses repeated 3 values, named string arrays, and identifiers such as corvid_num, M_PI, false, true, SIZE_MAX, and fortytwo.
What you can do after
Evaluate which repeated values require names and distinguish enumeration constants, macros, and temporary objects by role.
How it connects
Connects named constants with enumeration constants, macros, compile-time constants, temporary objects, and global constants.

Also explained in

Compare how 3 books explain constant →

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.