75

After all, both these statements do the same thing...

int a = 10;
int *b = &a;
printf("%p\n",b);
printf("%08X\n",b);

For example (with different addresses):

0012FEE0
0012FEE0

It is trivial to format the pointer as desired with %x, so is there some good use of the %p option?

2
  • 2
    By using '%p' you print the address of the variable in question, "The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx)."
    – user1831086
    Mar 3, 2010 at 10:26
  • In C++, you can use (void *) typecast: see stackoverflow.com/questions/5657123/…
    – kagali-san
    Apr 14, 2011 at 0:21

7 Answers 7

150

They do not do the same thing. The latter printf statement interprets b as an unsigned int, which is wrong, as b is a pointer.

Pointers and unsigned ints are not always the same size, so these are not interchangeable. When they aren't the same size (an increasingly common case, as 64-bit CPUs and operating systems become more common), %x will only print half of the address. On a Mac (and probably some other systems), that will ruin the address; the output will be wrong.

Always use %p for pointers.

4
  • 17
    %p will also use an adequate textural representation for pointer for the platform. On platforms where it is common to represent pointer in hex, this won't make a difference as long as the size is correct but for a segmented architecture (do you remember DOS?) it may use a segment:offset representation. Mar 3, 2010 at 8:23
  • 12
    One also needs to cast b to void * with the %p format - this is one of the few instances in C where one needs a cast. Mar 3, 2010 at 11:25
  • 1
    "All the world's a VAX!" syndrome.
    – David Cary
    Aug 22, 2011 at 20:30
  • Adding to @AlokSinghal comment: for GCC if you compile with the -Wextra flag you will get appropriate warnings for incorrect types in printf. To make the warning go away you need to use the correct form and cast to void* which can be helpful.
    – Fred
    Jul 23, 2018 at 22:35
9

At least on one system that is not very uncommon, they do not print the same:

~/src> uname -m
i686
~/src> gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
[some output snipped]
gcc version 4.1.2 (Gentoo 4.1.2)
~/src> gcc -o printfptr printfptr.c
~/src> ./printfptr
0xbf8ce99c
bf8ce99c

Notice how the pointer version adds a 0x prefix, for instance. Always use %p since it knows about the size of pointers, and how to best represent them as text.

1
  • "...that is not very uncommon". Nice :) Jun 25, 2018 at 23:37
5

You cannot depend on %p displaying a 0x prefix. On Visual C++, it does not. Use %#p to be portable.

3
  • 1
    Is this documented somewhere? I cannot find anything about # used in conjunction with %p. I have checked: msdn.microsoft.com/en-us/library/8aky45ct(v=vs.71).aspx cplusplus.com/reference/clibrary/cstdio/printf
    – Suma
    Sep 25, 2012 at 10:24
  • 1
    PC-Lint doesn't like it, but it works in Visual Studio. Unfortunately it prepends '0X' not '0x', making the actual value hard to read. Presumably this is due to a missing '%P' variant. All told, this format specifier suffers from the problem of all 'platform dependent' implementations, that you can't really control what you're going to get. I use '0x%p' at the risk of getting a double 0x0X at the start. Rather undermines using it, because you know exactly what you're getting with %x. Jan 9, 2015 at 10:32
  • This doesn't seem to be at all portable, even between VC versions. For example, VS 2013 (12.0.40629.00 update 5) prefixes '0X' for "%#p" but has no prefix for "%p". But VC 19.00.23506 doesn't prefix for either "%p" or "%#p". Then G++ 5.4.0 prefixes with "0x" for both "%p" and "%#p" (and using "%#p" gives a G++ warning). In short, you can't rely on the format of this at all.
    – james
    Feb 21, 2019 at 11:38
5

The size of the pointer may be something different than that of int. Also an implementation could produce better than simple hex value representation of the address when you use %p.

2

x is Unsigned hexadecimal integer ( 32 Bit )

p is Pointer address

See printf on the C++ Reference. Even if both of them would write the same, I would use %p to print a pointer.

1
  • yeah, I already went through that page. What I wanted to know was why would you use %p to print a pointer. Anyways, Peter Hosey's answer answers my doubt.
    – Moeb
    Mar 3, 2010 at 8:06
2

When you need to debug, use printf with %p option is really helpful. You see 0x0 when you have a NULL value.

1
  • 1
    On gcc (4.4.7, at least), a NULL value shows as (nil) -- including the parentheses. There seems to be a lot of implementation-specific variation in %p Jan 7, 2019 at 1:26
-2

x is used to print t pointer argument in hexadecimal.

A typical address when printed using %x would look like bfffc6e4 and the sane address printed using %p would be 0xbfffc6e4

1
  • 1
    No, %x is used to print a value of type unsigned int in hexadecimal. There's absolutely no guarantee that unsigned int is the same size as void *, and it's a bad idea to assume so in code.
    – unwind
    Oct 5, 2014 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.