Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Immutable (constant) values should be declared as enumeration constants, const-qualified objects (unmodifiable lvalues), or as a last resort, a #definemacro definitions using #define.

Wiki Markup
Enforcing invariants such as object immutability using existing language mechanisms helps ensures the safety and security of applications.  For example, ISO/IEC PDTR 24772 \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] recommends labeling parameters as constants to avoid unintentional modification.

In general, it is preferable to declare immutable values as const-qualified objects rather than as macro definitions. Using a const declared value means that the compiler is able to check the type of the object, the object has scope, and (certain) debugging tools can show the name of the object.  However, for integer constants, it is preferable to use an enumeration constant instead of a const-qualified object, as this eliminates the possibility of taking the address of the integer constant and does not require that storage is allocated for the value.

...

Wiki Markup
\[[Summit 05|AA. C References#Summit 05]\] [Question 10.5b|http://c-faq.com/cpp/constvsdefine.html]
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.3.2.1, "Lvalues, arrays, and function designators," Section 6.7.2.2, "Enumeration specifiers," and Section 6.10.3, "Macro replacement"
\[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "CSJ Passing parameters and return values"

...

02. Declarations and Initialization (DCL)      02. Declarations and Initialization (DCL)       DCL01-A. Do not reuse variable names in subscopes