...
This CUBE()
macro definition is non-compliant because it fails to parethesize the macro expansion.
Code Block |
---|
#define CUBE(X, Y, Z) (X) * (YX) * (ZX) int i = 3; int a = 81 / CUBE(i); |
...
Code Block |
---|
int a = 81 / i * i * i;
|
which evaluates as
Code Block |
---|
int a = ((81 / i) * i) * i); //* evaluates to 243 */ |
while the desired behavior is
Code Block |
---|
int a = 81 / ( i * i * i); //* evaluates to 3 */ |
Compliant Solution
By adding parentheses around each argument, this definition of parenthesizing the macro expansion, the CUBE()
macro expands correctly (when invoked in this situationmanner).
Code Block |
---|
#define CUBE(X) ((X) * (X) * (X)) int i = 3; int a = 81 / CUBE(i); |
...