MISRA Discussion Forums
10.1 violated - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.6 Types (https://forum.misra.org.uk/forumdisplay.php?fid=33)
+---- Thread: 10.1 violated (/showthread.php?tid=1194)



10.1 violated - minnu - 08-07-2015

#define NUM 9u
#define NUM2 8


/* gus32cmd is keyboard entry .numbers from 1 to 10*/

scanf("%d", &gus32cmd );

Switch(gus32cmd)
{
case:NUM
....
....
break;


case NUM2
..
..
break;

}

At lines case:NUM and case NUM2 I got following misra c error

"Rule 10.1 violated "implicit conversion of complex integer expression to a small sized integer is not allowed"
What correction I should do to avoid this error


Re: 10.1 violated - misra-c - 05-08-2015

The value of the case expression is implicitly converted to the type of the switch chooser ( C90 section 6.6.4.2). This implicit conversion is checked by rule 10.1.

The type of the switch chooser (gus32cmd) is not given in this example, but must be either signed or unsigned. If gus32cmd is signed, then "case NUM" will violate rule 10.1 as they have different signs. If gus32cmd is unsigned, then "case NUM2" will violate rule 10.1 as again the signs are different.