Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: s/c/getchar()/g;

...

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

In this example, the programmer has mistakenly omitted the comparison operator (see MSC02-A. Avoid errors of omission) from the conditional statement, which should be c getchar() != EOF. After macro expansion, the conditional expression is incorrectly evaluated as a binary operation: cgetchar()-1. This is syntactically correct, even though it is certainly not what the programmer intended.

...

Once this modification is made, the non-compliant code example no longer compiles because the macro expansion results in the conditional expression c getchar() (-1), which is no longer syntactically valid.

...

Code Block
bgColor#ccccff
#define EOF (-1)
/* ... */
if (cgetchar() != EOF) {
   /* ... */
}

Note that there must be a space after EOF because otherwise it becomes a function-like macro (and one that is incorrectly formed, since -1 cannot be a formal parameter).

...