What's the proper use of printf to display pointers padded with 0s

Florian picture Florian · Aug 10, 2009 · Viewed 35k times · Source

In C, I'd like to use printf to display pointers, and so that they line up properly, I'd like to pad them with 0s.

My guess was that the proper way to do this was:

printf("%016p", ptr);

This works, but this gcc complains with the following message:

warning: '0' flag used with ‘%p’ gnu_printf format

I've googled a bit for it, and the following thread is on the same topic, but doesn't really give a solution.

http://gcc.gnu.org/ml/gcc-bugs/2003-05/msg00484.html

Reading it, it seems that the reason why gcc complains is that the syntax I suggested is not defined in C99. But I can't seem to find any other way to do the same thing in a standard approved way.

So here is the double question:

  • Is my understanding correct that this behavior is not defined by the C99 standard?
  • If so, is there a standard approved, portable way of doing this?

Answer

AProgrammer picture AProgrammer · Aug 10, 2009

#include <inttypes.h>

#include <stdint.h>

printf("%016" PRIxPTR "\n", (uintptr_t)ptr);

but it won't print the pointer in the implementation defined way (says DEAD:BEEF for 8086 segmented mode).