Quiz Entry - updated: 2026.07.14
How do variables map to memory in C?
A variable name is just a human-friendly alias for an address the compiler picked; writing a really means "the memory cell at a's address", and the cell lives in stack, heap, data, or text depending on how it's declared.
* A variable name is an alias for the address the compiler assigned — writing a really means the cell at that address. *
* Where each kind of variable lives: the stack (locals), the heap (malloc), data/bss (globals and statics), and read-only text (code). *
int a = 5, b = 7, c;
Memory layout:
alias adr mem
28 [ ]
c 24 [ ? ] ← uninitialized (garbage value!)
b 20 [ 7 ]
a 16 [ 5 ]
12 [ ]
8 [ ]
4 [ ]
0 [ ]
Key insight: When you write a, the compiler translates it to mem[16]:
... = a + ... → ... = mem[16] + ...
a = ... → mem[16] = ...
Where data lives in the process memory:
| Segment | Contains | Lifetime |
|---|---|---|
| Stack | Local variables, function parameters | Automatic (function scope) |
| Heap | malloc()'d memory |
Manual (free()) |
| Data/BSS | Global and static variables | Program lifetime |
| Text/Code | Executable instructions | Read-only |
// Data segment
int global = 5;
// Read-only segment
const int readonly = 7;
// a, b on stack
int foo(int a, int b) {
// Stack
int local;
// Data segment (survives function calls)
static int persist;
// Pointer on stack, data on heap
char *m = malloc(10);
return local;
}
Tip: Visualize this with https://pythontutor.com/c.html
Go deeper:
Data segment (text / data / bss / heap / stack) — Wikipedia — how a running program's memory is carved into segments.