...
Once this modification is made, the non-compliant code example no longer compiles because the macro expansion results in the conditional expression getchar() (-1)
, which is no longer syntactically valid. 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).
Compliant Solution
The following In this compliant solution uses parentheses around the macro replacement list and adds the (previously omitted) comparison operator, we replace the macro definition with an enum
in compliance with DCL00-A. Declare immutable values using enum or const.
Code Block | ||
---|---|---|
| ||
#defineenum { EOF = (-1) }; /* ... */ if (getchar() != EOF) { /* ... */ } |
...
Exceptions
PRE02-EX1. A macro that expands to a single identifier or function call is not affected by the precedence of any operators in the surrounding expression, so its replacement list need not be parenthesized.
...