MISRA switch staement

Moderators: misra-c, david ward

Post Reply
Hanumanthappa E
Posts: 3
Joined: Wed Oct 11, 2017 6:04 am
Company: Panacea Medical Technologies Pvt Ltd

MISRA switch staement

Post by Hanumanthappa E » Wed Oct 18, 2017 10:20 am

Hi everyone,
Can anyone give an example of MISRA-C "switch" statement pls....

dg1980
Posts: 108
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: MISRA switch staement

Post by dg1980 » Wed Oct 18, 2017 12:01 pm

I assume you mean fully compliant, right?

Code: Select all

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*/
}

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Re: MISRA switch staement

Post by misra-c » Mon Nov 13, 2017 11:04 am

An example of a compliant switch statement is:

Code: Select all

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

Post Reply

Return to “8.16 Switch statements”