Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
essential type of constant expression
#2
Code:
uint_8 foo = (((uint_8)(900000Lu / 5000Lu)) - ((uint_8)(600000Lu / 5000Lu))) - 1u;
This can be best explained by working up the tree of operations. It is assumed that all values of the "unsigned char" type fit in the "signed int" type.

1. 900000Lu ( and other Lu literals)
The literal has a C standard type of "unsigned long", which has a rank greater than the rank of "int". Therefore the essential type is the same as the standard type which is "unsigned long".

2. 900000Lu / 5000Lu
The "/" operation is between 2 literals with a C type and ET of "unsigned long".
The resultant C type is "unsigned long", which has a rank greater than the rank of "int". Therefore the essential type is the same as the standard type which is "unsigned long".

3. (uint_8)(900000Lu / 5000Lu)
This casts a C "unsigned long" type to "unsigned char".
Casts are not listed in Appendix D.7 and so the essential type will be that of the cast. i.e "unsigned char".

4. (uint_8)(600000Lu / 5000Lu)
For the same reasons as 1 - 3 the essential type is "unsigned char"

5. (((uint_8)(900000Lu / 5000Lu)) - ((uint_8)(600000Lu / 5000Lu)))
The "-" operation is between two C types of "unsigned char".
C's unary promotion rule promotes each operand to "signed int" and therefore the C resultant type of the expression is "signed int".
As the C type of the expression is "signed int", the essential type is determined by the value of the expression.
The value of the expression is 60 and hence the UTLR and essential type is "unsigned char".

6. 1u
The literal has a C type of "unsigned int" and an essential type of "unsigned char".

7. (((uint_8)(900000Lu / 5000Lu)) - ((uint_8)(600000Lu / 5000Lu))) - 1u
The "-" operation is between the C types of "signed int" and "unsigned int", and the essential types of "unsigned char" and "unsigned char".
C's promotion rules converts the LH operand to "unsigned int", so the C resultant type of the expression is "unsigned int".
As the C type of the expression is "unsigned int", the essential type is determined by the value of the expression.
The value of the expression is 59 and hence the UTLR and essential type is "unsigned char".
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)