Skip to content
Codescope

Variables & memory addresses

A variable is a name you gave to a box the machine only knows by number.

intro7 min8 steps

Before you start

A computer's memory is one long strip of numbered boxes. Every box holds a number, and every box has an address — its position on the strip. That is all memory is.

A **variable** is a name your program gives to one of those boxes so you can talk about it in words rather than numbers. age, total, count — they all point at real boxes with real addresses.

This lesson watches the boxes appear as you declare variables, and shows you what your program is actually doing behind those familiar names.

Stack

one frame per running function
main()
no locals yet

The frame is reserved the moment the function is entered — before any line of its body runs.

Step 1 of 8. Every function gets a frame. When `main` starts running, the machine hands it a slab of stack memory called a *stack frame*. Every local variable you declare will live inside it. Right now the frame exists but nothing has been declared, so there is nothing in it.

01 / 08

Step 01 / 08

Every function gets a frame

When main starts running, the machine hands it a slab of stack memory called a *stack frame*. Every local variable you declare will live inside it.

Right now the frame exists but nothing has been declared, so there is nothing in it.

variables-and-memory.cC
#include <stdio.h>int main(void) {    int   score = 42;    char  grade = 'A';    float ratio = 0.5f;    printf("%d\n", score);    printf("%p\n", (void *)&score);    score = 99;    return 0;}