2

%X is used to print hexadecimal %p is used to print pointers, in hexadecimal

I understand that using %x instead of %p might result in instances where the values of the hexadecimal is truncated resulting in inaccuracies.

When printing hexadecimal with %p instead of %x, the only drawback is the huge numbers of 0s at the MSB.

So, is there any case where I cannot use %p to substitute the %x.

Peter
  • 437
  • 1
  • 4
  • 20

6 Answers6

3

You must first fully understand why the values can get truncated if you try to print an address using %x format specifier.

If you pass an address to printf there will be a certain number of bytes passed to the function. The size is defined by sizeof(void*). If you then tell printf you want to format is as an int value in hexadecimal format (by using %x) then printf will only read the number of bytes that are related to an int parameter, i.e.sizeof(int). (Actually a value of type unsigned int is expected. But this has same size as type int and when passing a value to printf that is of type int or smaller, it will only be converted to type int.) If those values do not match, there will be some excess bytes left that are fetched and interpreted when the next parameter shall be converted. Or there are less bytes available and printf will read invalid memory.

The same issue arises if you cheat printf in the opposite direction. Then you will pass only the number of bytes related to an int but printf will fetch the number of bytes in a pointer. Again this causes trouble.

Therefore just use the correct format specifier.

BTW: The formatting for %p is not necessarily done in hex format. If you have some CPU architecture where pointers are split in page/offset values, you might get something like ff80:0010 instead of a single hex number.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • basically using %p to print hex values would sometimes get unexpected outcomes due to their size? – Peter Aug 28 '20 at 02:51
  • Yes. Size may be different. And the bit representation of an address might not be a linear number and might be printed accordingly as I mentioned in last sentence of the answer. – Gerhardh Aug 28 '20 at 05:20
2

The %x format specifier expects an unsigned int as an argument. A pointer might not have the same size or representation as an unsigned int (or unsigned long or unsigned long long). Using the wrong format specifier invokes undefined behavior and must be avoided.

Additionally, not all pointers necessarily have the same representation, so you need to explicitly cast the pointer in question to void * when printing with %p.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

Do not use %p to print values of non-pointer types. Do not use %x to print values of pointer types. If the type of the argument does not match what the conversion specifier expects, then the behavior is undefined and the output can literally be anything.

The output of %p is implementation-defined; there are no flags to control its formatting.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

"What if I replace all the %X to %p in printf() method?"

%p is meant for pointers cast to (void *) only. If you pass an argument which relates to %p which is not a pointer and strictly seen also not a pointer cast to void * then the program invokes undefined behavior.

p - The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

Source: C18, §7.21.6.1/8 - "the fprintf function".


"So, is there any case where I cannot use %p to substitute the %x?"

In fact, in most cases you cannot do so. The only case is when you incorrectly have an argument of type void* to the %x format specifier. Then you would fix your old code becoming correct and proper by substituting %x with %p.

1

How the data is formatted via %p is implementation-defined. For example, you may get ugly result when you create hex-dumping software with %p:

For example, running this,

#include <stdio.h>

int main(void) {
    char data[64];
    /* read data from file in real application */
    for (int i = 0; i < 64; i++) data[i] = (char)i;

    puts("--- %x version ---");
    for (int i = 0; i < 64; i++) {
        printf(" %02X", (unsigned char)data[i]);
        if ((i + 1) % 16 == 0) putchar('\n');
    }

    puts("--- %p version ---");
    for (int i = 0; i < 64; i++) {
        printf(" %p", (void*)(unsigned char)data[i]);
        if ((i + 1) % 16 == 0) putchar('\n');
    }

    return 0;
}

You may get this:

--- %x version ---
 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
--- %p version ---
 (nil) 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf
 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f

In this experiment, 0x prefixes that I don't want are added in %p version and also 0 is printed as (nil) with %p.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

when using %p printf will condider the variable to be printed as unsigned long long (8 bytes) while when using %x printf will consider the variable as unsigned int (4 bytes), so you will notice no difference when using %p instead %x the extra bytes will just be filed with zeros, but in reverse using %x instead of %p it will omit the extra bytes and consider the variable as 4 bytes

bari barix
  • 68
  • 4
  • _when using %p printf will condider the variable to be printed as unsigned long long (8 bytes)_ No! `%p` will surely consider the current platform but whether `unsigned long long` has the same size like a pointer (aka `void*`) is a completely different story. Not to mention that pointer have e.g. only 4 bytes size when you compile for x86 (which is IMHO still common). – Scheff's Cat Aug 27 '20 at 14:54
  • got you maybe i wasn't precise and assumed always size unisgned long long and void* are the same (which is the case in my system architure) but is was to say that the problem is when pointer is represented with bytes more than unsigned int is represented with – bari barix Aug 27 '20 at 15:20
  • I once (in the past) assumed that pointer, `unsigned`, and `size_t` have always the same size. When we switched to x64, I very regretted my mis-belief... ;-) ...and had to fix a lot of code... – Scheff's Cat Aug 27 '20 at 19:44