Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Underlying type for integer constant expressions
#2
You are correct in stating that there is a conflict between Rule 10.1 and Section 6.10.4. This was not intentional.

You could write the expression as:

Code:
uint8_t d = (uint8_t)( 0x1U | 0x2U );

The underlying type of each of the constants, 0x1U and 0x2U, is unsigned int. The underlying type of the expression (0x1U | 0x2U) is also unsigned int. It is permitted to cast a constant expression because Rule 10.3 doesn't apply to constant expressions (Section 6.10.5) and, even if it did apply, a cast to a shorter unsigned type would be permitted by Rule 10.3.

An alternative would be to deviate Rule 10.1 in this situation.

The underlying type of integer constant expressions (as opposed to integer constants) is not handled properly in all cases in MISRA C 2004. The next revision of MISRA C will provide a more complete treatment of this subject.
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)