The C compiler has to expand the value passed to printf
(this is called "promotion"), because printf
is a variadic function (it can be called with differing arguments). For values of type char
, the promoted value is of type int
. Since your compiler's char
type seems to be signed, the promoted value is sign extended. In binary:
char i = 255 // or: 11111111 in binary
int promoted_i = -1 // or: 11....11111 (usually, 32 or 64 ones)
In the unsigned case, no sign-extension happens:
char u = 255 // or: 11111111 in binary, same as above but with different interpretation
unsigned int pu = i // or: 00....0011111111 (8 ones, preceded by the appropriate number of zeroes)