MISRA Discussion Forums

Full Version: MISRA switch staement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
Can anyone give an example of MISRA-C "switch" statement pls....
I assume you mean fully compliant, right?

Code:
switch (i)/* essentially integer (for bool use if/else), needs at least two clauses*/
{
  case 0:/* empty fallthrough compliant*/
  case 1:
    doSomething();/* statement or comment required*/
    break;/* unconditional break in every case clause required
  default:/* default case required as last (or first) clause*/
    /* statement or comment required*/
    break;/* unconditional break in default required*/
}
An example of a compliant switch statement is:
Code:
typedef signed char int8_t;
extern void use_int8(int8_t i);

void R_16_1 ( int8_t input )
{
    switch ( input )
    {
       case 0:
       {
          use_int8 ( input );
          break;
       }
       case 1:
          /* { .. } not required at this level */
          use_int8 ( -input );
          break;
          
       default:
       {
          use_int8 ( 0 );          
          break;
       }
    }
}
Other compliant examples can be seen in the example sections of rules 16.4, 16.5, and 16.6 in the MISRA C:2012 guidelines.