100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Variables in C++

Learn what are variables in C++, types of variables, declaration, initialization and examples with programs.

BasicsBeginner7 min readJul 7, 2026
Analogies

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

cpp
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

cpp
#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

text
Age: 20
Height: 5.9
Salary: 45000.5
Grade: A
Passed: 1

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#VariablesInC#Variables#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep