Page 1 of 1

15.1 and "switch(0) case 0:"

Posted: Tue Jan 10, 2012 3:01 pm
by gs
Hi,
Given the following code:

Code: Select all

...
switch(0)
case 0:
...
does the example presented violate rule #15.1? Syntactically, the "switch(expression)" is followed by a "statement" and not necessarily a "compound statement" as stated in the rule. Therefore, can we correctly presume said code violates said rule?

Re: 15.1 and "switch(0) case 0:"

Posted: Fri Jan 13, 2012 2:57 pm
by misra-c
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:

Code: Select all

void f (void) {
  switch (0)
  case 0: S;  /* violates 15.1 - containing compound statement is a function body */
}

Code: Select all

void g (void) {
  switch (1) {
  case 1: switch (0)
          case 0: { ... ; break; } /* complies with 15.1 but not 14.8 or 15.0 */
  }
}