Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
Difference between %p and %x

Difference between %p and %x

Apr 05 2013
Author:

Functions belonging to the printf function family have the type specifiers "%p" and "%x".

  • "x" and "X" serve to output a hexadecimal number. "x" stands for lower case letters (abcdef) while "X" for capital letters (ABCDEF).
  • "p" serves to output a pointer. It may differ depending upon the compiler and platform.

One specifier is often used instead of another on 32-bit systems, but it is a mistake. Here is an example:

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

On a Win32 system, the following result will be printed:

0018FF20
18FF20

As you may see, the output results for "%p" and "%X" are rather similar. This similarity leads to inaccuracy in the code and this, in turn, results in errors occurring when you port a program to a 64-bit platform. Most often it is "%X" that is used instead of "%p" to output the value of a pointer, and this results in printing of an incorrect value if the object is situated outside the four less significant Gbytes of the address space. Let us consider the corresponding 64-bit version of this program:

size_t Gb = 1024*1024*1024;
char *a = (char *)malloc(2 * Gb * sizeof(char));
char *b = (char *)malloc(2 * Gb * sizeof(char));
printf("use %%X: a=%X\n", a);
printf("use %%X: b=%X\n", b);
printf("use %%p: a=%p\n", a);
printf("use %%p: b=%p\n", b); 
use %X: a=80000040
use %X: b=40010040
use %p: a=0000000080000040
use %p: b=0000000140010040

The pointer value "b" is printed incorrectly when using "%X".

Here is one more example. Although it looks strange, the code given here in an abridged form was used in a real application in the UNDO/REDO subsystem:

// Here the pointers were saved in the form of a string
int *p1, *p2;
....
char str[128];
sprintf(str, "%X %X", p1, p2);
// In another function this string was processed
// in this way:
void foo(char *str)
{
  int *p1, *p2;
  sscanf(str, "%X %X", &p1, &p2);
  // The result is incorrect values of pointers p1 and p2.
  ...
}

Manipulation with the pointers using "%X" resulted in an incorrect program behavior on a 64-bit system. Note that such errors might occur very rarely. To diagnose these, and other similar flaws, it is a good idea to use the analyzer PVS-Studio.

References

Popular related articles


Comments (0)

Next comments next comments
close comment form
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam