For size_t
, assuming you have a sufficiently modern C library, use %zu
.
If you can’t use the z
modifier (some older libraries unfortunately don’t support it), cast to a wide-enough known type when printing, and then use a width specifier appropriate to that type:
size_t sz = sizeof(whatever);
...
printf("%lu", (unsigned long)sz);
This works as long as you’re never working with a size larger than 4 billion or so, i.e. that can fit in 32 bits. If you’re on a system where size_t
is 64 bits but long
is 32, you’ve theoretically got the problem of a size which size_t
can hold but %lu
can’t print. Whether this is a problem for you, and what to do if it is, is up to you. (The ideal solution, if your library supports it, is to go back to %zu
, which is the preferred solution and doesn’t have this problem in 32-bit, 64-bit, or any other sized environments. Or I guess you could use unsigned long long
and %llu
.)
2
solved C printf cross-platform format without warnings [duplicate]