Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Typecasting from signed to unsigned and vis-verse
#2
Rule 10.1 of MISRA C:2004 prohibits implicit conversions for signed to unsigned and vice versa

There are no restrictions on explicit casts between simple signed and unsigned expressions ( and vice versa ). For example:
Code:
UINT_8 u8 = ....
   SINT_8 s8 = ....
   u8 = s8;            /* violates 10.1 */
   u8 = (UINT_8)s8;   /* MISRA compliant */
Casting a complex expression requires the addition of an extra line.
Code:
SINT_8 stemp;  
   u8 = (UINT_8)(s8 + s8);    /* violates 10.3 */
   stemp = s8 + s8;
   u8 = (UINT_8)stemp;       /*  MISRA compliant */
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)