How variables are stored in RAM
What you'll learn: How your computer’s memory acts like a giant grid of numbered mailboxes to hold your program's data.
Memory is a giant grid
To understand pointers, you first need to understand how C sees your computer’s hardware. Think of your Random Access Memory (RAM) as a massive, organized wall of mailboxes in a post office.
Every single mailbox is exactly the same size—one byte (8 bits). Because there are billions of these mailboxes, the computer assigns a unique number to every single one so it can find them. This number is called a memory address.
What happens when you declare a variable?
When you write a line of code like int score = 10;, two specific things happen in your computer's memory:
- Reservation: The computer sets aside enough empty mailboxes to fit an integer. On most modern systems, an
intneeds 4 bytes (4 mailboxes). - Storage: The computer takes the value
10and stores it across those reserved mailboxes.
The name score is just a human-friendly label for those boxes. For the computer, the variable is defined by where it starts—its address.
Seeing the address in C
You can actually ask C to show you the "mailbox number" where a variable is living by using the Address-of Operator (&).
#include <stdio.h>
int main() {
int age = 25;
// Print the value stored in the variable
printf("Value: %d\n", age);
// Print the memory address where 'age' is stored
// %p is the format specifier for addresses (pointers)
printf("Memory Address: %p\n", (void*)&age);
return 0;
}
When you run this, you will see a strange-looking code like 0x7ffeed3c. This is a hexadecimal number representing the specific location in your RAM where the integer 25 is currently sitting.
Why this matters
In C, you aren't just moving numbers around; you are managing physical locations in hardware. A pointer is simply a variable that stores one of these mailbox addresses instead of a regular value. Understanding that every piece of data has a "home" with a specific address is the foundation of all advanced C programming.
Key takeaway
Every variable lives at a unique, numbered address in RAM, which you can find using the & operator.