MISRA Discussion Forums

Full Version: conversions with constant
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

natapush

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.
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.