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.