Versions Compared

Key

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

...

This recommendation is related to EXP10-A. Do not diminish the benefits of constants by assuming their values in expressions.

Non-Compliant Code Example (

...

Object-

...

Like Macro)

A preprocessing directive of the form

...

While inadequate in some ways, this is the best that can be done for non-integer constants.

Non-Compliant Code Example (

...

Immutable Integer Values)

In this non-compliant code example, max is declared as a const-qualified object. While declaring non-integer constants as const-qualified objects is the best that can be done in C, for integer constants we can do better. Declaring immutable integer values as const-qualified objects still allows the programmer to take the address of the object. Also, const-qualified integers cannot be used in locations where an integer constant is required, such as the value of a case constant.

...

Most C compilers allocate memory for const-qualified objects.

Compliant Solution (enum)

This compliant solution declares max as an enumeration constant rather than a const-qualified object or a macro definition.

...