...
This noncompliant code example contains multiple, unbound statements.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* * Swaps two values. * Requires tmp variable to be defined. */ #define SWAP(x, y) \ tmp = x; \ x = y; \ y = tmp |
...
This noncompliant code example inadequately bounds multiple statements.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* * Swaps two values. * 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 | ||||
---|---|---|---|---|
| ||||
if (x > y) { /* Single-branch if-statement!!! */ tmp = x; /* The one and only branch consists */ x = y; /* of the block. */ y = tmp; } ; /* emptyEmpty statement */ else /* ERROR!!! "parse error before else" */ do_something(); |
...
Wrapping the macro inside a do-while
loop mitigates the problem.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* * Swaps two values. * Requires tmp variable to be defined. */ #define SWAP(x, y) \ do { \ tmp = x; \ x = y; \ y = tmp; } \ while (0) |
...