Skip to content
FollowtheDamnCode

Variables

A named box that stores a value.

3 minute read

Big idea

A variable is a named box that holds a value.

The idea

To create one, write the type, then the name, then the value:

int age = 20;
  • int — the type: this box holds a whole number.
  • age — the name you'll use to get the value back.
  • 20 — what's inside the box right now.

You can change what's inside later: age = 21;

Example

Try it — edit it and run it again
#include <stdio.h>

int main(void) {
    int apples = 3;
    int oranges = 4;
    printf("%d\n", apples + oranges);
    return 0;
}
output
7

Run and edit it in the app.

%d is a placeholder: printf replaces it with the number after the comma.

Watch it

Think of it like

A labelled jar. The label is the name, and what's inside is the value. You can empty it and put something new in.

🕷️ Spidey-sense: I keep my gear in labelled pouches on my belt. A variable is one pouch: a name on the outside, one value inside. Swap what's inside anytime — the label stays the same.

Naming rules

  • Letters, digits, and _ only.
  • Can't start with a digit: 2cars ✗, cars2 ✓
  • No spaces: my age ✗, my_age ✓
  • Capitals matter: Age and age are two different variables.

Key terms

  • Variable — A named box that stores a value.
  • Declare — Create a variable: int age;
  • Initialize — Give it its first value: int age = 20;

Check yourself

What is a variable?

Which line creates a variable called age holding 20?

Which is NOT a valid variable name?