MISRA Discussion Forums
Implicit type convertion - 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: Implicit type convertion (/showthread.php?tid=268)



Implicit type convertion - fwamolina - 05-06-2006

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;


Re: Implicit type convertion - phaedsys - 07-06-2006

[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


- fwamolina - 07-06-2006

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;


- misra-c - 20-06-2006

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.