...
Code Block | ||||
---|---|---|---|---|
| ||||
int x, y, z, tmp; if (z == 0) tmp = x; x = y; y = tmp; |
Furthermore, this macro violates PRE02-C. Macro replacement lists should be parenthesized.
Noncompliant Code Example
This noncompliant code example parenthesizes its macro arguments, but inadequately bounds multiple statements:
Code Block | ||||
---|---|---|---|---|
| ||||
/* * Swaps two values and requires * tmp variable to be defined. */ #define SWAP(x, y) { tmp = (x); (x) = (y); (y) = tmp; } |
This macro fails to expand correctly in some case, such as the following example, which is meant to be an if
statement with two branches:
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* * Swaps two values and requires * tmp variable to be defined. */ #define SWAP(x, y) \ do { \ tmp = (x); \ (x) = (y); \ (y) = tmp; } \ while (0) |
The do-while
loop will always be executed exactly once.
...