A named constant gives an identifier to a value with a particular meaning. Naming prevents special values from being repeated throughout a program and makes later changes easier to maintain. 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]);
The listing repeats the value 3 for three related arrays and loop bounds, illustrating why each meaning should receive an explanatory name. section 5.6
Use these naming rules:
- Name every constant whose meaning matters. section 5.6
- Distinguish constants that happen to have equal values but serve different purposes. section 5.6
- Let a global constant identify a meaningful artifact, such as M_PI, false, true, SIZE_MAX, corvid_num, or fortytwo. section 9.2
C uses several mechanisms related to named or compile-time constants. Enumeration constants are proper named constants; macros provide text replacement and become compile-time constants only when their replacements consist of literals of base types. C23 also adds constexpr objects, while compound literals are unnamed temporary objects rather than named constants. section 5.6


