The identifier (format specifier) to print an address with printf
in C is
%p
. This specifier is used specifically for printing pointer (address)
values. When using %p
, the corresponding argument should be a pointer, and
it is recommended to cast the pointer to (void *)
for portability and
compliance with C99 and later standards. For example:
c
int a = 10;
printf("%p\n", (void *)&a);
This prints the address of the variable a
. Using other specifiers like %u
to print addresses is undefined behavior
and should be avoided
. The %p
specifier outputs the address in an implementation-defined format,
typically hexadecimal. In summary:
- Use
%p
to print addresses. - Pass the address cast to
(void *)
toprintf
. - Example:
printf("%p\n", (void *)&variable);
This is the standard and correct way to print an address with printf
in C.