Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MISRA switch staement
#1
Hi everyone,
Can anyone give an example of MISRA-C "switch" statement pls....
<t></t>
#2
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*/
}
<t></t>
#3
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.
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 3 Guest(s)