[MLton] Porting MLton... C99?
Thu, 9 Dec 2004 22:28:46 -0500 (EST)
> > All those printf("%08x", (uint)ptr); lines scattered everywhere are also a
> > problem. The best way to do this is to change it to printf("%08lx",
> > (ulong)z), no matter what type z is. When using uintX_t you have no idea
> > what that corresponds to in a format string so upgrading to the largest
> > portable formatted type is the best approach.
> I agree with the principle. But wouldn't using long long be better
> than long?
Actually inttypes.h (which is more wide-spread than stdint.h, I have
found) gives you string constants for format strings corresponding to all
of the [u]int[8|16|32|64]_t types. Here's an example of what it looks
like on my Mac:
#define PRId16 "hd"
#define PRIi16 "hi"
#define PRIo16 "ho"
#define PRIu16 "hu"
#define PRIx16 "hx"
#define PRIX16 "hX"
#define PRId32 "d"
#define PRIi32 "i"
#define PRIo32 "o"
#define PRIu32 "u"
#define PRIx32 "x"
#define PRIX32 "X"
... and so on, including 64-bit defines, and pointer defines. Although
it's rather wordy you can just write:
printf("%08" PRIxPTR, ptr);
and expect to get portable behavior.