What is the use of `%p` in printf in C?

In C, the %p format specifier is used with printf() to display pointer addresses in hexadecimal format. It provides a standard way to print memory addresses stored in pointer variables.

Syntax

printf("%p", pointer_variable);

Example: Basic Pointer Address Printing

Here's how to use %p to print the address of a variable −

#include <stdio.h>

int main() {
    int x = 50;
    int *ptr = &x;
    
    printf("The address is: %p, the value is %d<br>", ptr, *ptr);
    printf("Address of x using &x: %p<br>", &x);
    
    return 0;
}
The address is: 000000000022FE44, the value is 50
Address of x using &x: 000000000022FE44

Example: Different Pointer Types

The %p format specifier works with all pointer types −

#include <stdio.h>

int main() {
    int num = 100;
    char ch = 'A';
    float f = 3.14;
    
    int *int_ptr = &num;
    char *char_ptr = &ch;
    float *float_ptr = &f;
    
    printf("Integer pointer: %p<br>", int_ptr);
    printf("Character pointer: %p<br>", char_ptr);
    printf("Float pointer: %p<br>", float_ptr);
    
    return 0;
}
Integer pointer: 000000000022FE4C
Character pointer: 000000000022FE4B
Float pointer: 000000000022FE40

Key Points

  • The %p format displays addresses in hexadecimal format with implementation-defined formatting.
  • The actual address values will vary between program runs due to memory layout randomization.
  • Always use %p for pointers, not %x or %d, as pointer size may differ from int size.

Conclusion

The %p format specifier is the standard and portable way to print pointer addresses in C. It ensures correct formatting regardless of the underlying system architecture.

Updated on: 2026-03-15T10:33:23+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements