Search code examples
cpointersprintfmemory-addressformat-string

C how to print address as a decimal value (not hex) without compiler warnings


I have an int pointer that points at the address of another int.

When printing the pointer with the format specifier %p (as suggested by many answers on Stack Overflow), it's printed with a hexadecimal representation.

How can I print a pointer's value with with a decimal representation?


Code Sample

I have figured out one can maybe use %zu from this answer.

int i = 1;
int *p = &i;
printf("Printing p: %%p = %p, %%zu = %zu\n", p, p);

When I run that code using https://onlinegdb.com/EBienCIJnm

  • It actually prints the correct value in decimal
  • But it also outputs a compiler warning
warning: format ‘%zu’ expects argument of type ‘size_t’, but argument 3 has type ‘int *’ [-Wformat=]                                                                 
Printing p: %p = 0x7ffee623d8c4, %zu = 140732759529668

Is there a way to printf a pointer's value with a decimal representation, without compiler warnings?


Solution

  • Convert to the uintptr_t type defined in <stdint.h> and format with the PRIuPTR specifier defined in <inttypes.h>:

    #include <inttypes.h>
    #include <stdint.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        printf("%" PRIuPTR "\n", (uintptr_t) &argc);
    }