C, printf does not hold width when print in hex for negative int type

For example, following code create “ffffffff”, instead of “ff”

int a = -1;

printf(“02x”, a);

To solve this, use “hh”, which uses char format

printf(“02hhx”, a);

Leave a comment