Search code examples
cliteralsxc8

Is there a literal suffix for 8 Bit values?


I am working with the xc8 compiler and want to tell him that my literal is only 8 bit wide.

123 :no suffix, default int (16 bit in xc8)

123U :unsigned int also 16 bit wide

Any idea for a clean solution to describe a 8 bit literal?


Solution

  • There are no 8 bit literals in C. The closest thing you can get is UINT8_C(123) from stdint.h, which gives you the literal most suitable for a variable of type uint_least8_t. Very likely it will expand to 123U.

    As for how to solve this practically, you show the constant into a define, which you should be doing anyway:

    #define NUMBER_8BIT ( (uint8_t)123 )