Variables in memory addresses
What you'll learn: You will understand how C uses memory addresses to find and store the data held within your variables.
The Mailbox Analogy
When you create a variable in C, you aren’t just naming a value; you are claiming a physical piece of territory inside your computer’s RAM (Random Access Memory).
Think of your computer's memory as a massive, long street lined with millions of tiny mailboxes. Each mailbox is exactly the same size, and every single one has a unique, permanent house number painted on the side. In computing, that house number is called a memory address.
When you write int score = 100;, two things happen:
- The computer sets aside a specific "mailbox" (or a small cluster of them) to hold an integer.
- It links the name
scoreto that specific address.
From that point on, the name score is just a human-friendly nickname for a specific location in memory. When you ask for the value of score, the computer doesn't "look for a number"; it looks at the address linked to that name and reads whatever is sitting inside that mailbox.
Seeing the Address
While we usually care about the value inside the variable, C allows us to look at the address where that value lives. We do this using the "address-of" operator, which is the ampersand symbol (&).
#include <stdio.h>
int main() {
int age = 25;
// Printing the value stored in the variable
printf("The value is: %d\n", age);
// Printing the physical memory address where age lives
// %p is the format specifier for addresses (pointers)
printf("The memory address is: %p\n", (void*)&age);
return 0;
}
If you run this code, you will see a strange-looking code like 0x7ffd5e32. This is a hexadecimal number representing the exact "house number" in your RAM where the integer 25 is currently stored.
Why Addresses Matter
In many other languages, these addresses are hidden from you. C is powerful because it lets you work with them directly. Understanding that a variable is just a label for a physical location is the foundation for "Pointers," one of C's most important features. It means you aren't just juggling numbers; you are managing the actual hardware of your machine.
Key takeaway
A variable name is a human-friendly label for a specific physical address in your computer's memory.