Course contentsShow
← C
Lesson 237 of 1,2193. Operators and ExpressionsFree lesson

The Address-of operator `&`

The Address-of operator &

What you'll learn: You will learn how to find the specific location in your computer's memory where a variable is stored.

Memory is like a hotel

Imagine your computer’s memory (RAM) is a massive hotel with millions of rooms. Each room has a unique room number, like "Room 101" or "Room 5002." When you create a variable in C, like int age = 25;, the computer picks an empty room and puts the value 25 inside it.

Most of the time, we just care about the value (the guest in the room). But sometimes, we need to know the specific room number where that value lives. In C, we call this room number a memory address.

Meet the & operator

The ampersand symbol (&) is the Address-of operator. When you place it immediately in front of a variable name, it tells the computer: "Don't give me the value stored inside this variable; give me its address in memory instead."

Think of it like a GPS for your data. If age is the person, &age is their home address.

Seeing it in action

We can print a memory address using the printf function with a special format specifier: %p. The p stands for "pointer," which is C-speak for an address.

#include <stdio.h>

int main() {
    int score = 95;

    // Printing the value normally
    printf("The score is: %d\n", score);

    // Printing the address using the & operator
    // This will look like a strange code (e.g., 0x7ffd5e)
    printf("The memory address of score is: %p\n", &score);

    return 0;
}

When you run this, you will see a hexadecimal number (like 0x7ffee495). This number is the exact "mailbox" where the integer 95 is currently sitting. Every time you run the program, this address might change because the computer's "hotel manager" (the Operating System) might put the variable in a different empty room.

Why do we need this?

You might wonder why we care about the address. In later lessons, you’ll learn that knowing the address allows different parts of your program to share the same data without making messy copies. It is the first step toward understanding Pointers, one of the most powerful features of the C language.

Key takeaway

The & operator retrieves the unique memory address (the location) of a variable rather than the data stored within it.