Versions Compared

Key

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

...

  • operates at run time
  • consumes memory (though this is not too important)
  • can't use in compile-time constant expression
  • uses consistent syntax
  • can create pointers to
  • does type checking

Enumeration Constants

An enumeration constant is a member of an enumeration. Enumeration constant can be used to represent an integer constant expression that has a value representable as an int. Unlike const-qualified objects, enumeration constants do not require that storage is allocated for the value so it is not possible to take the address of an enumeration constant.

...

Wiki Markup
Using the {{sizeof}} expression reduces the total number of names declared in the program, which is almost always a good idea \[[Saks 02|AA. C References#Saks 02]\].  The {{sizeof}} operator is almost always evaluated at compile time (except in the case of variable length arrays).

Compliant Solution

Constant values that may be passed as compile-time arguments must be macro definitions, as shown by this example:

...

Replacing numeric constants with symbolic constants in this example does nothing to improve the readability of the code, and may in fact actually make the code more difficult to read:

...

When implementing recommendations, it is always necessary to use sound judgment.

(Note that this example does not prevent overflow or check for invalid operations (taking the sqrt() of a negative number.) See INT32-C. Ensure that operations on signed integers do not result in overflow and FLP32-C. Prevent or detect domain and range errors in math functions for more information on detecting domain and range errors in math functions.

Risk Assessment

Using numeric literals makes code more difficult to read and understand. Buffer overruns are frequently a consequence of a magic number being changed in one place (like an array declaration) but not elsewhere (like a loop through an array).

...