Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 10.5 clarification
#4
Integer constants can never be unsigned char or unsigned short.

ISO C 9899:1999 6.4.4.1:
Code:
Suffix      Octal or Hexadecimal Constant    

None        int
            unsigned int
            long int
            unsigned long int

u or U      unsigned int  
            unsigned long int

The smallest unsigned integer type (underlying type) in your first code example is uint16_t. As you can see by the standard cited above, it can never be unsigned char or unsigned short.

And yes, by rule 10.5 you must immediately cast the result into this operand in case uint16_t happens to be unsigned short. In that case, the integer promotions will jump in and possibly change signedness of the final expression. Again, this depends on "integer conversion rank" in the C standard, and not on the implemented size of unsigned short.

But if one typedef uint16_t as unsigned int, no cast is necessary. As you point out, that will however lead to portability issues on 32-bit machines. This is perhaps one of the biggest flaws in the C language, and there is really no perfect work-around.

Good practice is to keep the typedefs for integers in a separate header file, which has to be rewritten for each platform you port the code to.
<t></t>
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)