...
This CUBE() macro definition is noncompliant because it fails to parenthesize the replacement list.:
| Excerpt | ||
|---|---|---|
| ||
compliant=no,enclose=yes,compile=yes |
...
In this noncompliant code example, END_OF_FILE is defined as -1. The macro replacement list consists of a unary negation operator, followed by an integer literal 1.:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#define END_OF_FILE -1
/* ... */
if (getchar() EOF) {
/* ... */
}
|
...
Parenthesizing the -1 in the declaration of END_OF_FILE ensures that the macro expansion is evaluated correctly.:
| Code Block |
|---|
#define END_OF_FILE (-1) |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
enum { END_OF_FILE = -1 };
/* ... */
if (getchar() != END_OF_FILE) {
/* ... */
}
|
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.
| Code Block |
|---|
#define MY_PID getpid() |
PRE02-EX2: A macro that expands to an array reference using the array-subscript operator [], or an expression designating a member of a structure or union object using either the member-access . or -> operators.
...