Versions Compared

Key

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

...

Non-Compliant Code Example

The header <stdiostdio.h>h defines the BUFSIZ macro which expands to an integer constant expression that is the size of the buffer used by the setbuf() function. This non-compliant code example defeats the purpose of defining BUFSIZ as a constant by assuming its value in the following expression:

...

The programmer's assumption underlying this code is that "everyone knows that BUFSIZ equals 512," and right-shifting nine bits is the same (for positive numbers) as dividing by 512. However, if BUFSIZ changes to 1024 on some systems, modifications are difficult and error - prone.

Compliant Solution

This compliant solution uses the identifier assigned to the constant value in the expression.

...

Hardwiring constants renders code potentially nonportable; in fact, it will produce unexpected behavior under any circumstances in which the constant changes.

...