Versions Compared

Key

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

...

This CUBE() macro definition is noncompliant because it fails to parenthesize the replacement list.

Excerpt
hiddentrue

compliant=no,enclose=yes,compile=yes

Code Block
bgColor#FFcccc
langc
#define CUBE(X) (X) * (X) * (X)
int i = 3;
int a = 81 / CUBE(i);

As a result, the invocation

Code Block
bgColor#FFcccc
langc
int a = 81 / CUBE(i);

expands to

Excerpt
hiddentrue

compliant=no,enclose=yes,compile=no

Code Block
bgColor#FFcccc
langc
int a = 81 / i * i * i;

which evaluates as

Code Block
bgColor#FFcccc
langc
int a = ((81 / i) * i) * i);  /* evaluates to 243 */

...

With its replacement list parenthesized, the CUBE() macro expands correctly for this type of invocation.

Code Block
bgColor#ccccff
langc
#define CUBE(X) ((X) * (X) * (X))
int i = 3;
int a = 81 / CUBE(i);

...

In this noncompliant code example, EOF is defined as -1. The macro replacement list consists of a unary negation operator, followed by an integer literal 1.

Code Block
bgColor#FFcccc
langc
#define EOF -1
/* ... */
if (getchar() EOF) {
   /* ... */
}

...

In this compliant solution, the macro definition is replaced with an enumeration constant in compliance with recommendation DCL00-C. Const-qualify immutable objects. In addition, since EOF is a reserved macro defined in the <stdio.h> header, the compliant solution must also use a different indentifier in order to comply with rule DCL37-C. Do not declare or define a reserved identifier.

Code Block
bgColor#ccccff
langc
enum { END_OF_FILE = -1 };
/* ... */
if (getchar() != END_OF_FILE) {
   /* ... */
}

...