...
This CUBE()
macro definition is noncompliant because it fails to parenthesize the replacement list.
Excerpt |
---|
|
compliant=no,enclose=yes,compile=yes |
Code Block |
---|
|
#define CUBE(X) (X) * (X) * (X)
int i = 3;
int a = 81 / CUBE(i);
|
As a result, the invocation
Code Block |
---|
|
int a = 81 / CUBE(i);
|
expands to
Excerpt |
---|
|
compliant=no,enclose=yes,compile=no |
Code Block |
---|
|
int a = 81 / i * i * i;
|
which evaluates as
Code Block |
---|
|
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 | lang | c#ccccff |
---|
|
#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 |
---|
|
#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 | lang | c#ccccff |
---|
|
enum { END_OF_FILE = -1 };
/* ... */
if (getchar() != END_OF_FILE) {
/* ... */
}
|
...