Consider this little program:
#include <stdio.h>
int main()
{
char c = 0xFF;
printf("%d\n", c);
return 0;
}
Its output is -1, as expected (considering char is signed in my
system). What I'm trying to do is to make it print 255. This is of
course a simplification of the real situation, where I can't just define
c as unsigned.
The first possible change would be using %u as formatter instead, but
the usual type promotion rules apply here, and the number is printed as
232 - 1.
So is there any way to read the signed char as unsigned before it gets
promoted to an int? I could create a pointer to a unsigned char set to the
address of c, and dereference it later, but not sure if this is the best
approach.
cis being promoted using signed promotion rules. Castcto unsigned to use unsigned promotion.Unsigned char will be promoted to unsigned int.