Opening and closing braces for if
, for
, or while
statements should always be used, even if said statement's body contains only a single statement.
In the event that either of these statements are used in a macro, this implies that the macro definition should not be concluded with a semicolon (see PRE11-C. Do not conclude macro definitions with a semicolon).
Braces help improve the uniformity, and therefore readability of code.
...
Macros can be used to execute a sequence of multiple statements as group.
Note that the following macro violates a macro with multiple statements should be wrapped in a do-while loop (see PRE10-C. Wrap multi-statement macros in a do-while loop), but for the purposes of this example it is not. However, the situation can still be salvaged if braces are used in the if
statement.
Code Block | ||
---|---|---|
| ||
#define DEC(x,y) \ printf("Initial value was %d\n", x); \ x -= y; \ printf("Current value is %d\n", x) |
...
Code Block | ||
---|---|---|
| ||
int x, y, z; if (z == 0) { printf("Initial value was %d\n", x); x -= y; printf("Current value is %d\n", x) } |
Noncompliant Code Example
Code Block | ||
---|---|---|
| ||
Risk Assessment
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP19-C | medium | probable | medium | P8 | L2 |
...