Why is the format specifier for uint8_t and uint16_t the same (%u)?

Rev picture Rev · Oct 14, 2014 · Viewed 72.9k times · Source

I only found pretty unrelated questions due to the tons of results searching for printf().

Why does uint8_t not specify its own format string but any other type does?

As far as I understand printf(), it has to know the length of the supplied parameters to be able to parse the variable argument list.

Since uint8_t and uint16_t use the same format specifier %u, how does printf() "know" how many bytes to process? Or is there somehow an implicit cast to uint16_t involved when supplying uint8_t?

Maybe I am missing something obvious.

Answer

wick picture wick · Oct 14, 2014

Because %u stands for "unsigned", it well may be uint64_t and is architecture dependent. According to man 3 printf, you may want to use length modifier to get sought behaviour, i.e. %hu (uint16_t) and %hhu (uint8_t).