printf format specifiers for uint32_t and size_t

ant2009 picture ant2009 · Jul 2, 2010 · Viewed 219k times · Source

I have the following

size_t   i = 0;
uint32_t k = 0;

printf("i [ %lu ] k [ %u ]\n", i, k);

I get the following warning when compiling:

format ‘%lu’ expects type ‘long unsigned int’, but argument has type ‘uint32_t’

When I ran this using splint I got the following:

Format argument 1 to printf (%u) expects unsigned int gets size_t: k

Many thanks for any advice,

Answer

kennytm picture kennytm · Jul 2, 2010

Try

#include <inttypes.h>
...

printf("i [ %zu ] k [ %"PRIu32" ]\n", i, k);

The z represents an integer of length same as size_t, and the PRIu32 macro, defined in the C99 header inttypes.h, represents an unsigned 32-bit integer.