[previous] MISRA-C:2004  Rule  15.0:  (Required) [next] The MISRA C switch syntax shall be used.
The syntax for the switch in C is weak, allowing complex, unstructured behaviour. The following text describes the syntax for switch statements as defined by MISRA-C and is normative. This, and the associated rules, enforces a simple and consistent structure on the switch statement.

The following syntax rules are additional to the C standard syntax rules. All syntax rules not defined below are as defined in the C standard.

switch-statement:

    switch ( expression ) { case-label-clause-list default-label-clause }

case-label-clause-list:

    case-label case-clauseopt
    case-label-clause-list case-label case-clauseopt

case-label:

    case constant-expression :

case-clause:

    statement-listopt break ;
    { declaration-listopt statement-listopt break ; }

default-label-clause:

    default-label default-clause

default-label:

    default :

default-clause:

    case-clause

and

statement:

    /* labelled_statement removed */
    compound_statement
    expression_statement
    selection_statement
    iteration_statement
    /* jump_statement removed or just restricted to return depending on other rules */

The following terms are also used within the text of the rules:

switch labelEither a case label or default label.
case clauseThe code between any two switch labels.
default clauseThe code between the default label and the end of the switch statement.
switch clauseEither a case clause or a default clause.

Example Code:


#include "misra.h"
#include "m2cmex.h"

extern S16 test_1500( void )
{
   switch ( s16a )
   {
      static S16 s16x1500;              /* MISRA Violation */
      case 1:
         s16x1500 = s16a;
         break;
      case 2:
         s16b = s16x1500;
         break;
      default:
         break;
   }
   return 1;
}


QAC messages that encompass this guideline:

3234 Declarations precede the first label in this 'switch' construct.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents