1. Intro
In C++, a variable is a named memory location used to store data. The value stored in a variable can be changed during program execution — that's what makes it "variable" rather than fixed. Every variable in C++ has a data type, a name, and a value.
Cricket analogy: A variable is like a player's named jersey slot on the scoreboard - the runs displayed there (its value) change over the innings, but the named slot itself (like 'Kohli') stays fixed throughout the match.
2. Syntax
data_type variable_name = value;3. Explanation
Here, data_type specifies the type of data the variable can store (such as int, float, or char). variable_name is the identifier you choose for the variable, and value is the initial value assigned to it. C++ is a statically typed language, so every variable must be declared with a specific type before it can be used — and once declared, a variable can only hold values of that type.
Cricket analogy: int runs = 50; is like registering a player as a specific role (data_type: batsman), giving them a name (variable_name: Kohli), and recording today's score (value: 50) - and once registered as a batsman, they can't suddenly bowl instead, just as a statically typed variable keeps its declared type.
A variable can be declared without an initial value (int age;) and assigned later (age = 20;), or declared and initialized in a single statement (int age = 20;). Using an uninitialized variable before assigning it a value produces unpredictable (garbage) results, so it is good practice to always initialize variables when you declare them.
Cricket analogy: Declaring int score; without a value is like naming a new batsman on the team sheet before they've faced a ball - reading their 'score' before the first delivery gives meaningless garbage, unlike int score = 0; which starts them at a known state.
Pro Tip
variables also have scope — a local variable declared inside a function only exists within that function, while a global variable declared outside any function is accessible throughout the file.
4. Example
#include <iostream>
using namespace std;
int main() {
int age = 20; // Integer variable
float height = 5.9; // Float variable
double salary = 45000.50; // Double variable
char grade = 'A'; // Character variable
bool isPassed = true; // Boolean variable
cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
cout << "Salary: " << salary << endl;
cout << "Grade: " << grade << endl;
cout << "Passed: " << isPassed << endl;
return 0;
}5. Output
Age: 20
Height: 5.9
Salary: 45000.5
Grade: A
Passed: 16. Key Takeaways
- Variables are used to store data.
- Each variable has a specific data type.
- Variables must be declared before use.
- The value stored in a variable can be changed.
Practice what you learned
1. What is a variable in C++?
2. What must every variable have in C++?
3. What is the output of `bool isPassed = true; cout << isPassed;`?
4. Which of these correctly declares and initializes a variable?
5. What happens if you use a variable before assigning it a value?
Was this page helpful?
You May Also Like
Data Types in C++
Learn the fundamental data types in C++ — int, float, double, char, bool, and void — with sizes, ranges, and examples.
Constants in C++
Learn what constants are in C++, the different ways to define them (const, #define, constexpr), and when to use each.
Identifiers in C++
Learn what identifiers are in C++, the naming rules the compiler enforces, and good naming conventions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics