portable printf
I was squashing some warnings in [uzbl]{tags/uzbl} code:
printf("set %s = %d\n", (char *)k, (int)*c->ptr);
Ignoring the fact that, at first glance, it’s weird to cast a pointer to int
(ptr
is defined as void**
), compiling this code on 64bit would warn you
that you were casting a pointer to an int. That’s because a pointer is 64bit
and an int is 32bit.
To fix that it was rewritten to:
printf("set %s = %d\n", (char *)k, (intptr_t)*c->ptr);
Where intptr_t
is c99’s way of representing an integer type that can hold
a pointer. On 64bit this becomes a long long
.
This of course complains about printing a 64bit integer with %d
. And that’s the
real reason for this post. I always forget the magi for this.
The <inttypes.h>
header file was added by c99 to provide common macros for
handling types that change on different architectures. For this excercise
we need the PRIuPTR
macro which defined as either ld
or d
, on 64bit and
32bit respectively.
The fixed line looks like this:
printf("set %s = %"PRIuPTR"\n", (char *)k, (intptr_t)*c->ptr);
… and now that I wrote about it, I hope I will remember.