Posts: 632
Threads: 18
Joined: Jan 2006
Reputation:
1
The exception was created for signed literals as coders frequently omit the "U" suffix and it seemed over harsh to prevent such assignments.
If a coder has added the suffix "U", this explicitly signals an intention that the literal should be "unsigned" and therefore should only used in an
essentially unsigned context. The view of the working group was that assigning such literals to a signed object could indicate a programming error and should therefore raise a violation.
Code:
int8_t s8a = 1u; /*Non-compliant */
Posted by and on behalf of the MISRA C Working Group
Posts: 632
Threads: 18
Joined: Jan 2006
Reputation:
1
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