%x
is only for printing unsigned int
, however you supply a char
.
Using %x
with a negative value of char
causes undefined behaviour.
Aside: The C Standard specification of printf
is not particularly clear; some feel that passing anything except exactly an unsigned int
causes undefined behaviour. Others (including myself) feel that it's OK to pass arguments that are not specifically unsigned int
, but after the default argument promotions, have type int
with a non-negative value. The standard does guarantee that non-negative int
s have the same representation as the unsigned int
with the same value.
Some of the other answers suggest %hhx
, but that is not any better than %x
. The standard (on a sensible interpretation) specifies that %hhx
only be used with an unsigned char
argument, and %hhd
only be used with a signed char
argument. There is actually no modifier for plain char
.
Either way you look at it, nowhere can printf
be used to convert negative values to positive representations in a well-defined manner. You must convert the argument yourself and then use a matching format specifier. In this case:
printf ("%hhx\n", (unsigned char)testchar);
would be one option. IMO %x
could be used here, but as mentioned above, some disagree.
NB. The wrong format specifier is used in printf ("%d\n", sizeof(char));
and the line following that. The specifier for size_t
is %zu
. So you could either use %zu
, or cast the argument to int
, or even better:
printf("1\n");