Combining case and default?

6.15 Switch Statements

Moderators: misra-c, david ward

Post Reply
Posts: 3
Joined: Mon Jul 23, 2007 3:53 pm
Location: Colorado, USA

Combining case and default?

Post by exoson » Fri Aug 07, 2009 5:21 pm

I would be interested in comments on the following simplified fragment.

switch (err) {
case 0:

case 1:
case 2:
case 3:

I am seeing code examples like this where multiple cases are being combined with default. Most compiler will simply optimize this out to a simple test for case 0. The programmers are using this as a way to identify the results they might expect. I think having the other cases in a comment would serve this as well. MISRA C does not seem to mention this.
From a test perspective I feel it is necessary to test all declared cases.
Can anyone offer a comment on this?

Validated Software

Posts: 571
Joined: Thu Jan 05, 2006 1:11 pm

Re: Combining case and default?

Post by misra-c » Wed Sep 09, 2009 10:54 am

The code fragment does not violate any MISRA C rules relating to the switch statement.

The number of tests cases suggested by this code fragment will depend on the project testing strategy. This is not something that the MISRA C Guidelines addresses.
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.15 Switch Statements”