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
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
- What is a Variable? — A variable is a named box in the computer’s memory that holds a value. You give it a name (like 'age' or 'score'), and the computer sets aside a little chunk of memory for it. You can store a value in it, read the value, or change it to a new value. Variables are how your program remembers things — without them, your code would forget everything instantly!
- int — Whole Numbers — The 'int' type stores whole numbers (no decimal points). Examples: 0, 1, 42, -7, 1000000. An int can hold numbers from about -2 billion to +2 billion. Use int for counting things: number of students, number of problems solved, your score, ages, quantities. In CP, int is the type you will use most often!
- long long — Really Big Numbers — Sometimes int is not big enough! If a problem says the answer can be up to 10^18 (a billion billion), you need 'long long'. It can hold numbers up to about 9.2 quintillion (9,200,000,000,000,000,000). In CP, a good rule is: if numbers might exceed 2 billion, use long long. Writing 'long long' is a bit long, but your code will handle huge numbers safely.
- double — Decimal Numbers — The 'double' type stores numbers with decimal points, like 3.14, 99.99, or -0.5. Use double when you need precision — for example, calculating averages, distances, or anything with fractions. Be careful: doubles are not 100% precise due to how computers store decimals, so avoid comparing doubles with == in CP. Use a tiny tolerance instead.
- char — Single Characters — The 'char' type stores a single character — one letter, one digit, or one symbol. You write char values in single quotes: 'A', 'z', '7', '#'. Each char is actually stored as a number (its ASCII code). For example, 'A' is 65 and 'a' is 97. This is useful for problems involving letters, like checking if something is uppercase or counting vowels.
- string — Text — A 'string' stores text — a sequence of characters. You write strings in double quotes: "Hello", "competitive programming", "abc123". Strings can be empty (""), have one character ("A"), or be very long. You can access individual characters with square brackets: if s = "Hello", then s[0] is 'H' and s[1] is 'e'. Strings are essential for text-based CP problems!
- bool — True or False — The 'bool' type stores just two values: true or false (yes or no). It is named after George Boole, a mathematician who invented this idea. Use bool for conditions: is the number even? Is the student passing? Has the game ended? In C++, true is stored as 1 and false as 0.
- Declaring and Initializing Variables — Declaring a variable means creating it: 'int age;' creates a box called age. Initializing means giving it a starting value: 'int age = 15;' creates the box AND puts 15 in it. Always initialize your variables! An uninitialized variable contains garbage — a random leftover value from memory — and using it is a common source of bugs.
- Naming Rules and Good Habits — Variable names must start with a letter or underscore, and can contain letters, digits, and underscores. They cannot have spaces or special characters. Names are case-sensitive — 'Score' and 'score' are different variables. Use descriptive names: 'totalScore' is better than 'x'. In CP, short names like n, m, a, b are fine because the context is clear and you need to type fast.
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. #include and using namespace std; — our standard CP setup. Gives us all the tools we need.
- 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. int score = 100; — another integer variable. We can have as many variables as we need.
- 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. double pi = 3.14159; — a decimal number. Double gives us about 15 digits of precision, which is enough for most CP problems.
- 6. char grade = 'A'; — a single character in single quotes. Remember: single quotes for char, double quotes for string!
- 7. string name = "Alex"; — text goes in double quotes. A string can hold zero or more characters.
- 8. bool isStudent = true; — a true/false value. Note: no quotes around true — it is a keyword, not text.
- 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. 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?
Show answer
Explain like I'm 5
Fun fact
Hands-on challenge
More resources
- C++ Variables — CP-Algorithms Reference (CP-Algorithms)
- USACO Guide — Data Types in C++ (USACO)
- C++ Variables and Data Types Explained Simply (YouTube)