How should I print types like off_t and size_t?

Georg Schölly picture Georg Schölly · Feb 25, 2009 · Viewed 142.9k times · Source

I'm trying to print types like off_t and size_t. What is the correct placeholder for printf() that is portable?

Or is there a completely different way to print those variables?

Answer

Johannes Schaub - litb picture Johannes Schaub - litb · Feb 25, 2009

You can use z for size_t and t for ptrdiff_t like in

printf("%zu %td", size, ptrdiff);

But my manpage says some older library used a different character than z and discourages use of it. Nevertheless, it's standardized (by the C99 standard). For those intmax_t and int8_t of stdint.h and so on, there are macros you can use, like another answer said:

printf("value: %" PRId32, some_int32_t);
printf("value: %" PRIu16, some_uint16_t);

They are listed in the manpage of inttypes.h.

Personally, I would just cast the values to unsigned long or long like another answer recommends. If you use C99, then you can (and should, of course) cast to unsigned long long or long long and use the %llu or %lld formats respectively.