1

When I use printf to print pointer values, they display in the format: 000000000066FE14. I would like them to display in the 0x format, such as: 0x66FE14.

This is the simple test program I wrote to print pointer values:

#include <stdio.h>

int main() {
    int x = 4;
    int *xAddress = &x;

    printf("Address of x: %p\n", xAddress);
    return 0;
}

This produces the output:

Address of x: 000000000066FE14
Jr795
  • 691
  • 1
  • 8
  • 15
  • Do you mean "How do I trim off the leading 0 values in a pointer represented as a hex string?" Tip: Use `sprintf` and trim away! – tadman Jul 24 '19 at 21:55
  • I guess so. As printf uses formatting I was wondering if there was a certain format specifier that did it, hence the wording. – Jr795 Jul 24 '19 at 21:57
  • 1
    I think it's going to put leading 0 values in there for you, they're not optional like on `%d` and such. Maybe `sprintf`, then use that buffer offset to the first non-zero value. – tadman Jul 24 '19 at 21:58
  • Strictly, you should coerce `xAddress` into a `void *` before using it with `printf()` and `%p`. In this context, you could simply use `void *xAddress = &x;` with no explicit cast needed. In many contexts, you'd need to use `(void *)xAddress` in the argument list to `printf()`. – Jonathan Leffler Jul 24 '19 at 23:55
  • 1
    Note that some systems would print the hex address with a `0x` or `0X` prefix — the `%p` conversion specification is allowed to do that, but it is also allowed to omit them (as on your system). If you want portable behaviour, you'll probably convert to `uintptr_t` and then format with that and `PRIXPTR` or `PRIxPTR`, etc. Note that in theory, there might not be a type `uintptr_t` (an unsigned integer type that can hold any valid pointer to an object — pointers to functions have separate rules). In practice, I've not heard of a system where it is unavailable. – Jonathan Leffler Jul 24 '19 at 23:59

6 Answers6

5

Try using the uintptr_t type:

#include <stdio.h>
#include <inttypes.h>
#include <stdint.h>

int main() {
    int x = 4;
    int *xAddress = &x;

    printf("Address of x: 0x%" PRIXPTR "\n", (uintptr_t) xAddress);
    return 0;
}
Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
5

The C standard does not define the format used by the %p specifier; it is left to each implementation to define. To control the formatting of an address, you can convert it to the uintptr_t type and print that. To do this, include the <inttypes.h> and <stdint.h> headers, in addition to the <stdio.h> header. Then convert the pointer to uintptr_t with a cast and use the PRIxPTR or PRIXPTR macros (for lowercase or uppercase, respectively).

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>


int main(void)
{
    int a;
    printf("0X%" PRIXPTR "\n", (uintptr_t) &a);
}

This will print the pointer using a hexadecimal format. Whether that hexadecimal value corresponds to a hardware memory address is not guaranteed by the C standard, although the conversion from a pointer to an integer is intended to be unsurprising to a person familiar with the architecture.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
2

When run in this editor, it looks like the program is printing your pointer in hex format. %p is also the convention for printing pointers. That being said, if you want to format your printing to be in the 0x format, you'd likely be interested in this answer, which gives some tips on how to print in hex format.

Maybe cast the pointer's address to a (void *) first? Could be causing implementation-specific issues if your pointer type is (int *)

printf("Address of x: %p\n", (void *)xAddress);

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
0
printf("Address of x: 0x%x\n", xAddress);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jesse J
  • 532
  • 1
  • 7
  • 18
  • The behavior of using `%x` with a pointer is not defined by the C standard, per C 2018 7.21.6.1 9: “… If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.” – Eric Postpischil Jul 24 '19 at 22:06
  • @EricPostpischil It would be possible after converting it to uintptr_t, however – Ctx Jul 24 '19 at 22:10
  • @Ctx but the conversion itself is implementation defined, so there is no saying what would be printed. – Ajay Brahmakshatriya Jul 25 '19 at 02:00
0

To printf as hex output, you have to add the 0x prefix yourself:

printf("0x%08x\n", xAddress);

This will print xAddress, prefixed with 0x, and then filled with leading 0 and the address for 8 digits. If you know you want exactly 6 digits, and you don't want the leading 0, you can use 0x%6x.

anonmess
  • 465
  • 3
  • 9
  • The behavior of using `%x` with a pointer is not defined by the C standard, per C 2018 7.21.6.1 9: “… If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.” – Eric Postpischil Jul 24 '19 at 22:06
0

printf("0x%016llx", (void* __ptr64)xAddress); //visualstudio