MISRA Discussion Forums

Full Version: Implicit type convertion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My question is why my code test program (lint) generate an error when read this line.

U16 u16value;
U8 u8value;

u16value = (U16) u8value / 10U;

I think that line is absolutly compliance with misrac.

but in other hand this other line does not generate an error, but i think that it is not misra compliance.

U16 u16value;
U8 u8value;

u8value = (U16) u8value / (U16)10U;
[quote="fwamolina"]My question is why my code test program (lint) generate an error when read this line.

U16 u16value;
U8 u8value;

u16value = (U16) u8value / 10U;

I think that line is absolutly compliance with misrac.
[quote]

1 are you using MISRA C 1 or 2

2 What is the error lint gives?

3 what rule does it think is being broken
I think that because U16 is a small type and misra2004 in section 6.10.4 about integer constants said that an 10U constant is handle as U32 type, so that it will not compliance with 6.10.6. is that right?

My lint use misra 1998.

Also i want to ask if there is an error in the second line, i mean can i cast from an U32 to U8?

U8a = (U32) u8b / (U32) u8c;
In section 6.10.4, we indicate that unsigned values between 0 and 255 are treated as having an underlying type of unsigned 8 bits.

Therefore 10U is treated as having an underlying type of unsigned 8 bit .

Your code example

Code:
U16 u16value;
U8 u8value;

u16value = (U16) u8value / 10U;

should not generate any warnings.

Your code example

Code:
U16 u16value;
U8 u8value;

u8value = (U16) u8value / (U16)10U;

should generate a warning because a U16 expression is being narrowed into a U8 result - 10.1a.


Code:
U8 u8a;
U8 u8b;
U8 u8c;

U8a = (U32) u8b / (U32) u8c;

should generate a warning because a U32 expression is being narrowed into a U8 result - 10.1a.

Writing just

Code:
U8a = u8b / u8c;

is compliant. The result of a U8 expression is stored in a U8 variable.

This is an area were MISRA-C:2004 is significantly different than MISRA-C:1998.