Skip to main content
Be Bitwise

What Are Variables?

Variables & Data Types · Coding Fundamentals track

6 min read

In this module

From Memory Addresses to Names

The CPU modules showed instructions and data sitting in memory at numbered addresses, raw numbers like 0x7FFF5A2C. That works for the CPU, but writing programs in nothing but addresses would be miserable. Variables fix this by giving friendly names to memory locations.

A variable is a named container with three essential properties: a name you choose, a value it currently holds, and a type that describes what kind of value it can store.

text
age = 25

In this single line we create a variable called age, give it the value 25, and (depending on the language) the system infers or enforces that it holds a number. Behind the scenes, the compiler or interpreter picks a memory address, stores the binary representation of 25 there, and remembers that whenever you say age you mean that address.

Declaration vs. Assignment

Most languages separate two ideas:

  • Declaration tells the system that a variable exists and what type it is.
  • Assignment stores a value in that variable.

Some languages fold these into a single step. Others want you to declare first and assign later.

text
// Declaration and assignment together
let temperature = 98.6
// Declaration first, assignment later
let name
name = "Alice"

The key insight: in a compiled language like C, declaring a local variable reserves space for it (usually a slot in the function's stack frame, which the memory modules cover later), and assignment writes a value into that space. If you declare a variable and never assign it, the memory still exists. It just contains whatever bits happened to be there before. That's called an uninitialised variable, and reading from one is a classic source of bugs. Some languages set variables to a default value (zero, null) upon declaration; others leave the memory contents undefined.

You can see the reserved space directly in C. Each variable below gets an address and a size fixed by its type; run it a couple of times and compare.

Try itThe sizes never change between runs. The addresses may, because the OS randomises where the stack starts.
C (GCC 9.2.0)
What would int count; printf("%d", count); print in C?

Anything. count is a local with no initialiser, so it holds whatever bytes were left in that stack slot. You might see 0, you might see a large random-looking number, and it can change between builds. The C standard calls reading it undefined behaviour, and compilers warn about it with -Wall, which is a good reason to always build with warnings on.

Key Concept

A variable is an abstraction over a memory address. It lets you refer to stored data by a human-readable name instead of a numeric address. The compiler or interpreter handles the mapping between names and addresses for you. This mapping is one of the most foundational services a programming language provides.

Why Names Matter

Variable names are for humans, not computers. The CPU never sees the name age. It only sees the address and the bytes stored there. But good names make programs readable:

text
// Hard to understand
x = y * z
// Clear intent
total_cost = price * quantity

Every language has rules about what makes a valid variable name. Most require that names start with a letter or underscore, contain only letters, digits, and underscores, and aren't the same as a reserved keyword (like if or while). Languages also differ on case sensitivity. In most, Age, age, and AGE are three different variables.

Naming conventions vary by language and community. Python programmers typically use snake_case (like total_cost), while JavaScript and Java programmers use camelCase (like totalCost). These are conventions, not rules enforced by the language, but following them keeps your code consistent with the ecosystem.

Scope: Where Variables Live

A variable doesn't live forever, and it isn't visible everywhere. Scope is the region of a program where a variable can be accessed.

  • Local variables exist only inside the function or block where they were declared. When that function returns, the variable is destroyed and its memory is reclaimed.
  • Global variables are reachable from anywhere in the program. They persist for the entire lifetime of the program.
text
function greet() {
let message = "hello" // local to greet
print(message) // works fine
}
greet()
print(message) // ERROR: message does not exist here

This ties into the stack and heap, which you'll meet properly in the memory modules. Local variables typically live on the stack and are automatically cleaned up when the function's stack frame is popped. Global variables live in a dedicated data segment that sticks around for the program's lifetime.

Most experienced programmers keep global variables to a minimum because they create hidden dependencies between different parts of a program. When any function can read or modify a global, it becomes a chore to reason about what state the program is in at any given moment.

Constants: Variables That Don't Vary

Sometimes you want a named value that should never change. A constant is declared once and then locked.

text
const PI = 3.14159265
const MAX_USERS = 1000

Constants help prevent accidental bugs. Try to reassign one and the compiler stops you. They also serve as documentation: when you see const, you know the value is intentionally fixed and you can rely on it staying put.

Tying It Back to the Hardware

Every variable you declare ultimately becomes bytes in memory. An integer might take 4 bytes, a floating-point number 8 bytes, a boolean sometimes as little as 1. The compiler decides how much memory to reserve based on the variable's type, aligns it to the right boundary for efficient access, and emits machine instructions to read and write those bytes whenever your code references the variable name.

When you write age = 25, the compiler might emit something like MOV DWORD [rbp-4], 25, an instruction that stores the 32-bit integer 25 at an offset from the base pointer. The name age doesn't appear anywhere in the compiled binary. It's been replaced by a stack offset the CPU can work with directly.

Step through it below. The stack addresses are made up; the bytes are what an x86-64 machine really stores for 25, least significant byte first.

int age = 25, in bytes

Starts at 0x7ffc1000; small numbers under each byte are the last two hex digits of its address.

rbp = 0x7ffc1008

The function starts. The compiler has set aside 4 bytes for age just below rbp; whatever was there before is still there.

Step 1 of 3

Understanding variables at this level, as named typed references to specific chunks of memory, is what bridges the hardware you've been studying and the software you're about to write. Next lesson, we'll dig into the different types of data variables can hold, starting with numbers.