You must a format specifier that matches the type of the value you are printing.
%p
expects a void *
.
%x
expects an unsigned int
.
%lx
expects an unsigned long
.
This is documented in man 3 printf
.
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
int main(void) {
const char *s = "foo";
printf("%p\n", s);
printf("%s\n", PRIxPTR);
printf("%#" PRIxPTR "\n", (uintptr_t)s);
}
Output:
$ gcc -Wall -Wextra -pedantic -std=c99 a.c -o a && a
0x5b72da1774
lx
0x5b72da1774
PRIxPTR
will be something like x
. In my case, it’s lx
because a pointer is the same size as an unsigned long
.
2
solved I want to know about print type explicitly [closed]