Code:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h> // this includes stdint.h, too
int main()
{
void *p = &p; // holds address of itself
// The usual way.
printf("%p\n", p); // format p prints void* in hex
// Integer version of pointer value (needs stdint.h).
uintptr_t u = (uintptr_t)p;
// Another way (needs special format spec from inttypes.h).
printf("0x%" PRIxPTR "\n", u);
// What you are trying to do.
printf("0x");
for (int i = sizeof u * 2; i-- > 0; )
printf("%x", (unsigned)((u >> (i * 4)) & 0xf));
putchar('\n');
// C23 has UINTPTR_WIDTH which gives the bit width of uintptr_t,
// so instead of sizeof u * 2 we could say UINTPTR_WIDTH / 4.
// sizeof u * 2 assumes 8-bit bytes.
// If you want to assume a 64-bit pointer, then you could do this:
long long n = (long long)p;
printf("0x%016llx\n", n);
// Or this:
printf("0x");
for (int i = 16; i-- > 0; )
printf("%x", (unsigned)((n >> (i * 4)) & 0xf));
putchar('\n');
return 0;
}