MISRA Discussion Forums
conversions with constant - 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.10 Arithmetic Type Conversions (https://forum.misra.org.uk/forumdisplay.php?fid=37)
+---- Thread: conversions with constant (/showthread.php?tid=91)



conversions with constant - natapush - 17-11-2005

why are the following expressions are compliant/not compliant

u8a = 5U - compliant(u8a=u16b - not compliant at the same time)
u8a = 5UL not compliant

it is not a conversion to a wider integer type of the same signedness as in 10.1 a)
It is more an implicit conversion from wider to narrower types.
And why in one case it is compliant, and in another one not compliant.


- Gavin McCall - 05-01-2006

ANSWER: MISRA C Steering team 5/1/06

See page 40 in MISRA-C:2004

u8a = 5U;

The underlying type of 5U ( a constant), is the smallest unsigned integral type into which it fits. This is U8 (unsigned 8-bit) and therefore this value CAN be assigned to u8a.

u8a=u16b;

The underlying type of u16b is U16 (unsigned 16-bit). The assignment involves an implicit conversion to a narrower type and violates rule 10.1.

u8a = 5UL;

The underlying type of 5UL ( a constant), is unsigned long U32. The assignment involves an implicit conversion to a narrower type and violates rule 10.1.