Joakim Hove <> writes:
> I have code which makses use of variables of type size_t. The code is
> originally developed on a 32 bit machine, but now it is run on both a
> 32 bit and a 64 bit machine.
>
> In the code have statements like this:
>
> size_t buffer_size;
> printf("Total buffer size: %ud bytes \n",buffer_size);
I think you mean "%u", not "%ud". "%d" is for (signed) int; "%u" is
for unsigned int.
> Now the format "%ud" works nicely on the 32 bit computer; it actually
> works on the 64 bit computer as well, but the compiler spits out
> warning message. On the 64 bit computer it would have prefered:
>
> printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */
The format for unsigned long is "%lu".
In C90, the best way to print a size_t value is to convert it
to unsigned long and use "%lu":
printf("Total buffer size: %lu bytes\n", (unsigned long)buffer_size);
C99 adds a 'z' modifier specifically for size_t:
printf("Total buffer size: %zu bytes\n", buffer_size);
but many printf implementations don't support it. (Even if your
compiler supports C99 and defines __STDC_VERSION__ appropriately,
that's not, practically speaking, a guarantee that the library also
conforms to C99.)
Even in C99, the "%lu" method will work unless size_t is bigger than
unsigned long *and* the value being printed exceeds ULONG_MAX, which
is unlikely to happen in practice.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.