Essential Type of an Expression Clarification - 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:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21) +---- Forum: 8.10 The essential type model (https://forum.misra.org.uk/forumdisplay.php?fid=165) +---- Thread: Essential Type of an Expression Clarification (/showthread.php?tid=1027) |
Essential Type of an Expression Clarification - gs - 14-02-2014 Suppose I have: Code: ((unsigned short) 1u) + ((unsigned short) 2u); Re: Essential Type of an Expression Clarification - misra-c - 31-03-2014 Appendix D.7 for "+" states in paragraph 2.1 that Quote: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.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". The MISRA Essential Types model produces an internally consistent view of C types, but a side-effect of keeping the model simple has been that a few integer constant expressions (where integer promotions are involved) may produce results that at first sight are not obvious. For those interested in the detailed calculation of the Essential Type for an expression, there are two basic situations that are useful to remember.
This second situation is particularly important since Appendix D.1 states that Quote: 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. The following examples aim to clarify the application of the essential type rules to expressions containing either casts or literals with suffices. unsigned int u1, u2; // assuming 16 bit short and 32 bit int Code: 5U; : Essential Type (ET) = unsigned char Code: (unsigned short)5U; : ET = unsigned short The standard type is "unsigned short", and so according to Appendix D.1, the essential type is the same as the standard type. (Appendix D.7 is not applied) Code: (unsigned int)5U; : ET = unsigned int Quote:The essential type of any expression not listed in this section is the same as its standard typeCasts are not mentioned in Appendix D.7, and so the essential type is the same as the standard type. Code: 2U + 3U; : ET = unsigned char The ET is determined by Appendix D.7 on "+" 2.1. Quote: If the expression is an integer constant expression then the essential type of the result is the UTLR of the result.The unsigned type of lowest rank (UTLR) for the value 5 is unsigned char. Code: (unsigned short)(2U + 3U); : ET = unsigned short Code: (unsigned short)2U + (unsigned short)3U; : ET = unsigned char The ET is determined by Appendix D.7 on "+" 2.1, The UTLR for the value 5 is unsigned char. Code: (unsigned long)2U+(unsigned long)3U; : ET = unsigned long Code: 2UL + 3UL; : ET = unsigned long Code: (unsigned short)u1 + (unsigned short)u2; : ET = unsigned short The ET is determined by Appendix D.7 on "+" 2.2, which states that Quote:the essential type of the result is the essential type of the operand with the highest rank.In this case both operands have an ET of unsigned short. Code: (unsigned short)50000U + (unsigned short)50000U; : ET = unsigned int The ET is determined by Appendix D.7 on "+" 2.1, The UTLR for the value 100,000 is unsigned int. |