MISRA Discussion Forums

Full Version: Typecasting from signed to unsigned and vis-verse
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Please can anybody help me to understand whether Misra C rules support signed to unsigned and vis-verse data typecasting?

If there is requirement to do some manipulation on signed and unsigned values, then i think it is ok to type-caste. But the requirement given to us says that whenever there is typecasting happening from signed to unsigned and vis-verse, then such incident should be failed.

Thanks in advance,
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:
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.
SINT_8 stemp;  
   u8 = (UINT_8)(s8 + s8);    /* violates 10.3 */
   stemp = s8 + s8;
   u8 = (UINT_8)stemp;       /*  MISRA compliant */