Course contentsShow
← C
Lesson 450 of 1,2197. PointersFree lesson

Printing addresses with `%p`

Printing addresses with %p

What you'll learn: How to use the address-of operator and the %p format specifier to see exactly where data lives in your computer's memory.

Your computer's mailboxes

Imagine your computer’s memory (RAM) as a massive, long street filled with billions of tiny mailboxes. Each mailbox holds a piece of data, like a number or a character. To keep things organized, every single mailbox has a unique house number or "address."

In C, when you create a variable, the computer picks a mailbox for it. Usually, we just use the variable's name to get the data, but sometimes we want to know the actual house number. This is the foundation of understanding pointers.

The & and %p tools

To see a memory address, we need two tools:

  1. The Address-of Operator (&): Placing an ampersand in front of a variable name tells C, "Don't give me the value inside; give me the address of the mailbox."
  2. The %p Format Specifier: Just as we use %d for integers, we use %p (short for pointer) to print memory addresses.

Seeing it in action

When you print an address, it won't look like a normal number like 10 or 500. Instead, it will look like a strange string of numbers and letters, such as 0x7ffcc822. This is a "hexadecimal" value, which is the standard way programmers read memory locations.

#include <stdio.h>

int main() {
    int myAge = 25;

    // Print the value stored in the variable
    printf("Value: %d\n", myAge);

    // Print the memory address where the variable lives
    // We use &myAge to get the address and %p to display it
    printf("Address: %p\n", (void*)&myAge);

    return 0;
}

Note: The (void*) in the code above is a small technical requirement to tell printf to treat the address as a generic location.

If you run this code, you’ll see the value 25, followed by a complex code. If you run it again, the address might stay the same, or it might change! This is because the computer chooses any available "mailbox" every time the program starts.

Why this matters

You don't need to memorize these long hexadecimal addresses. However, seeing them proves that your variables aren't just names—they are real physical locations in your hardware. Learning how to find these addresses is your first step toward manipulating memory directly.

Key takeaway

Use the & operator to find a variable's address and the %p specifier to print it to the console.