Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
10.3 Clarification on assigning unsigned literal to signed
#4
The result of the cast "(int8_t)256U" is implementation-defined since 256 can not be represented by a signed 8-bit type. A tool supporting directive 1.1 may generate a warning in order to direct the user to document the behaviour of such a cast.

We would recommend removing the "U" rather than adding a cast.
Code:
int8_t s8a = 256;  // implicit narrowing conversion violates rule 10.3
An alternative would be to use a mask so that the value always fits the int8_t range.
Code:
int8_t s8a = (int8_t)(256U & 0xffU);
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)