Code: Select all
((unsigned short) 1u) + ((unsigned short) 2u);
Moderators: misra-c, david ward
Code: Select all
((unsigned short) 1u) + ((unsigned short) 2u);
Casts from arithmetic types to integer types are permitted in integer constant expressions and so the expression satisfies the conditions of 2.1. Therefore the essential type of the result will be "unsigned char".if both operands are essentially unsigned, then if the expression is an integer constant expression then the essential type of the result is the UTLR of the result.
The following examples aim to clarify the application of the essential type rules to expressions containing either casts or literals with suffices.The essential type of an expression only differs from the standard C type in expressions where the standard type is either signed int or unsigned int.
Code: Select all
5U; : Essential Type (ET) = unsigned char
Code: Select all
(unsigned short)5U; : ET = unsigned short
Code: Select all
(unsigned int)5U; : ET = unsigned int
Casts are not mentioned in Appendix D.7, and so the essential type is the same as the standard type.The essential type of any expression not listed in this section is the same as its standard type
Code: Select all
2U + 3U; : ET = unsigned char
The unsigned type of lowest rank (UTLR) for the value 5 is unsigned char.If the expression is an integer constant expression then the essential type of the result is the UTLR of the result.
Code: Select all
(unsigned short)(2U + 3U); : ET = unsigned short
Code: Select all
(unsigned short)2U + (unsigned short)3U; : ET = unsigned char
Code: Select all
(unsigned long)2U+(unsigned long)3U; : ET = unsigned long
Code: Select all
2UL + 3UL; : ET = unsigned long
Code: Select all
(unsigned short)u1 + (unsigned short)u2; : ET = unsigned short
In this case both operands have an ET of unsigned short.the essential type of the result is the essential type of the operand with the highest rank.
Code: Select all
(unsigned short)50000U + (unsigned short)50000U; : ET = unsigned int