13-02-2022, 09:58 AM
C performs different integer promotions on balancing operands ( e.g. > ) as compared to the operands of assigning conversions, which include switch cases.
For balancing operators, each operand is first promoted using the "integer promotion" rules and then the two operands are balanced to a common type using the "usual arithmetic conversions".
For assigning operators, the RH operand (or case expression) is converted to the type of the LH operand (or the promoted switch chooser)
For Example - assuming 8 bit char, 16 bit short and 32 bit int
Rule 10.3 on assigning operations has different exceptions to rule 10.4 on balancing operations because the conversions in C are quite different. The C type of an integer constant expression will effect the resultant C type of a balancing conversion, but it will not effect the resultant type of an assignment.
For balancing operators, each operand is first promoted using the "integer promotion" rules and then the two operands are balanced to a common type using the "usual arithmetic conversions".
For assigning operators, the RH operand (or case expression) is converted to the type of the LH operand (or the promoted switch chooser)
For Example - assuming 8 bit char, 16 bit short and 32 bit int
Code:
unsigned char u8;
u8 * 32767
// u8 is promoted to signed int, 32767 has type signed int.
// Operation takes place in signed int leading to undefined behaviour if u8 > 1
u8 * 32767U
// u8 is promoted to signed int, 32767U has type unsigned int.
// Operation takes place in unsigned int which leads to a defined wrapped value if u8 > 1
Posted by and on behalf of the MISRA C Working Group