Lesson 4 of 50 beginner

Variables — Your Code’s Memory Boxes

Learn how your program remembers things — meet the labeled jars of coding!

Open interactive version (quiz + challenge)

Real-world analogy

Variables are like labeled jars in a kitchen. Imagine you have jars labeled 'Sugar', 'Flour', and 'Salt'. Each jar holds something specific, and the label tells you what is inside. In C++, a variable is a labeled jar in the computer’s memory — you give it a name (the label), a type (what kind of stuff it holds), and you can put values in it, look at what is inside, or replace the contents anytime!

What is it?

A variable is a named location in the computer’s memory that stores a value. It has three key parts: a type (what kind of data it holds — numbers, text, true/false), a name (how you refer to it in code), and a value (the actual data stored). Variables let your program remember information, perform calculations, and make decisions based on stored values.

Real-world relevance

Variables are everywhere in real life, even outside programming! A scoreboard in a game has variables: homeScore and awayScore. A thermostat has a variable: currentTemperature. Your phone battery indicator is a variable. A shopping cart total is a variable that changes every time you add an item. Any piece of information that can change is essentially a variable!

Key points

Code example

#include <bits/stdc++.h>
using namespace std;

int main() {
    // Integer — whole numbers
    int age = 12;
    int score = 100;

    // Long long — really big numbers
    long long bigNumber = 1000000000000LL;

    // Double — decimal numbers
    double pi = 3.14159;
    double average = 87.5;

    // Char — single character
    char grade = 'A';
    char initial = 'J';

    // String — text
    string name = "Alex";
    string greeting = "Hello, Competitive Programmer!";

    // Bool — true or false
    bool isStudent = true;
    bool hasPassed = false;

    // Print them all out!
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Grade: " << grade << endl;
    cout << "Score: " << score << endl;
    cout << "Big Number: " << bigNumber << endl;
    cout << "Pi: " << pi << endl;
    cout << "Is Student: " << isStudent << endl;

    // You can change variables!
    score = score + 50;
    cout << "New Score: " << score << endl;

    return 0;
}

Line-by-line walkthrough

  1. 1. #include and using namespace std; — our standard CP setup. Gives us all the tools we need.
  2. 2. int age = 12; — we create a variable called 'age' of type int (whole number) and store the value 12 in it. The = sign here means 'put this value inside the box.'
  3. 3. int score = 100; — another integer variable. We can have as many variables as we need.
  4. 4. long long bigNumber = 1000000000000LL; — the LL at the end tells the compiler this is a long long number, not an int. Without LL, the compiler might get confused by such a big number.
  5. 5. double pi = 3.14159; — a decimal number. Double gives us about 15 digits of precision, which is enough for most CP problems.
  6. 6. char grade = 'A'; — a single character in single quotes. Remember: single quotes for char, double quotes for string!
  7. 7. string name = "Alex"; — text goes in double quotes. A string can hold zero or more characters.
  8. 8. bool isStudent = true; — a true/false value. Note: no quotes around true — it is a keyword, not text.
  9. 9. cout << "Name: " << name << endl; — we print a label ("Name: ") followed by the value stored in the name variable. The << operator chains multiple things together.
  10. 10. score = score + 50; — this takes the current value of score (100), adds 50, and stores the result (150) back into score. The old value is replaced. You can also write this as 'score += 50;' as a shortcut.

Spot the bug

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a = 1000000;
    int b = 1000000;
    int result = a * b;
    cout << result << endl;
    return 0;
}
Need a hint?
A million times a million is one trillion. What is the maximum value an int can hold? Is one trillion within that range?
Show answer
The bug is integer overflow! The variable 'result' is an int, which can only hold values up to about 2 billion. But a * b = 1,000,000 * 1,000,000 = 1,000,000,000,000 (one trillion), which is way beyond what int can store. The fix is to use 'long long result = (long long)a * b;' or declare a and b as long long from the start. Integer overflow is one of the trickiest bugs in CP!

Explain like I'm 5

Imagine you have a bunch of labeled boxes in your room. One box is labeled 'Toys' and it holds your toys. Another is labeled 'Books' and holds books. A small one labeled 'Lucky Coin' holds just one coin. In coding, these labeled boxes work the same way — each box has a name (the label), a rule for what it can hold (only numbers, only words, etc.), and stuff inside (what you stored). You can open a box, look inside, or swap the stuff out!

Fun fact

The name 'boolean' (bool) comes from George Boole, an English mathematician from the 1800s who had almost no formal education! He taught himself math and went on to create Boolean algebra — the math of true and false — which became the foundation of ALL modern computers. Every phone, laptop, and game console works because of ideas from a self-taught man from 200 years ago!

Hands-on challenge

Write a program that creates variables to describe yourself: your name (string), your age (int), your height in meters (double), the first letter of your name (char), and whether you like pizza (bool). Print all of them in a nice format. Then change your age to your age next year and print the updated age. Try it on ideone.com!

More resources

Open interactive version (quiz + challenge) ← Back to course: CP Zero to Hero