I’m trying to find a way to make an enum “unsigned”.
enum{
x1 = 0,
x2,
x3
};
uint8_t = x2; /* <--- PC-LINT MISRA-C 2004 will complain about mixing signed and unsigned here */
Of course, I can add a typecast to get rid of the error, that is time consuming and error prone.
uint8_t = (uint8_t)x2; /* This works, but is a lot of extra work over the course of 1000s of lines of code*/
So, is there a way to make a specific enum unsigned that MISRA-C 2004 will like?
There is no standard C way to control the type chosen for an
enum. You can do it in implementation specific ways sometimes, like by adding a value to the enumeration that forces the type to be unsigned:But that’s not even standard C, either (since the value provided won’t fit in an
int). Unfortunately, you’re pretty much out of luck. Here’s the relevant bit from the standard:You might be better off using
#definerather thanenumto make your constants: