Variables in C++

In C++, a variable is a named storage location in memory that can hold a value. Unlike constants, the value of a variable can be changed during the execution of a program.

A variable acts as a container to store data values such as numbers, characters, or strings.
For example, int age = 20; creates a variable named age.

Syntax:

data_type variable_name = value;

Why Use Variables?

  • To store and manage data during program execution.
  • To make programs flexible and reusable.
  • To perform operations on stored data (e.g., calculations, comparisons).

Rules for Naming Variables

  1. Variable names must begin with a letter (a–z, A–Z) or underscore (_).
  2. Subsequent characters can include letters, digits, or underscores.
  3. No spaces or special characters are allowed.
  4. C++ is case-sensitiveAge and age are different.
  5. Variable names should be meaningful for better readability.

Understanding these rules in more detail:

While declaring variables in C++, we must follow these rules:

  1. Variable names must begin with a letter (a–z, A–Z) or underscore (_)

    Valid example:

    _valid = 100;

    name = "Angel";

    Invalid example:

    2name = 50;   // Invalid: starts with a number
    @age = 20;    // Invalid: starts with special character
  2. Variable names can only contain letters, digits, and underscores

    Valid example:

    marks_1 = 95;

    roll_number = 25;

    Invalid example:

    my-var = 20;    // Invalid: contains hyphen
    my var = 100;   // Invalid: contains space
  3. C++ is case-sensitive

    This means Age and age are different variables.

    Example:

    int Age = 18;
    int age = 20;
    
    cout << "Age = " << Age << endl;
    cout << "age = " << age << endl;

    Output:

    Age = 18

    age = 20

  4. Variable names should not be C++ reserved keywords

    Examples of reserved words: int, class, return, for

    Example:

    int = 100;    // Invalid: 'int' is a keyword
    class = 5;    // Invalid: 'class' is a keyword
  5. Variable names should be meaningful

    Prefer total_marks instead of tm for better readability.

Example Program: Simple Variable Usage

// Simple program to demonstrate variables in C++
#include <iostream>
using namespace std;

int main() {
    int number = 10;   // Variable declaration and initialization
    cout << "Initial value of number: " << number << endl;

    // Changing the value of variable
    number = 25;
    cout << "Value of number after change: " << number << endl;

    return 0;
}

Output:

Initial value of number: 10

Value of number after change: 25

Explanation:

  • int number = 10; → A variable named number is declared and its initial value is 10.
  • cout is used to display the value of the variable on the screen.
  • number = 25; → The old value of number is replaced with 25. This shows that variable values can be changed any time.
  • endl is used after cout so that the next output comes in a new line.
  • In short: A variable is like a container in memory. We can store a value in it and change that value whenever needed.

Real-Life Analogy:

Think of a variable as a box with a label on it. You can put something inside the box, take it out, or replace it with something else. Similarly, in C++, variables are containers that can hold and change values during program execution.

Common Mistakes with Variables

  • Using a variable without initializing it first.
  • Declaring variables with confusing names (like x1, x2 instead of marks, total).
  • Forgetting that C++ is case-sensitive (e.g., Numnum).

Summary:

  • Variables are containers for storing data values that can change during program execution.
  • C++ variables are typed, meaning their type (int, float, string, etc.) must be specified.
  • Variable names follow specific rules and cannot use reserved keywords.
  • Best practice: use clear, meaningful names and initialize variables properly.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar