13-01-2012, 02:57 PM
The standard does not require the "body" of the switch statement to be a compound statement, so it would indeed by syntactically permitted to write the example given.
However, Rule 14.8 requires that the body be a compound statement. Further, Technical Corrigendum 1 introduced Rule 15.0 which specifies a more restricted form of syntax for the switch statement and this also requires that the body of a switch statement be a compound statement. Therefore, in the presence of Rules 14.8 and 15.0, the example does violate Rule 15.1.
Note that even if Rules 14.8 and 15.0 were deviated, the example given would violate Rule 15.1 unless the containing compound statement is also a switch body. For example:
However, Rule 14.8 requires that the body be a compound statement. Further, Technical Corrigendum 1 introduced Rule 15.0 which specifies a more restricted form of syntax for the switch statement and this also requires that the body of a switch statement be a compound statement. Therefore, in the presence of Rules 14.8 and 15.0, the example does violate Rule 15.1.
Note that even if Rules 14.8 and 15.0 were deviated, the example given would violate Rule 15.1 unless the containing compound statement is also a switch body. For example:
Code:
void f (void) {
switch (0)
case 0: S; /* violates 15.1 - containing compound statement is a function body */
}
Code:
void g (void) {
switch (1) {
case 1: switch (0)
case 0: { ... ; break; } /* complies with 15.1 but not 14.8 or 15.0 */
}
}
Posted by and on behalf of the MISRA C Working Group