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?
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?
Any idea for a clean solution to describe a 8 bit literal?
C only has 2 literals: string literals and compound literals (C99). C identifies 123
and 123u
as integer constants, not literals.
To form a C 8 bit literal: make a compound literal
#define OneTwoThree ((uint8_t) { 123 })
printf("%zu %x\n", sizeof(OneTwoThree), OneTwoThree);
// expected output
1 123
IDK if xc8 supports compound literals.
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 )