Variables in C++

A variable is a named memory location used to store data in a program. Think of it as a container that holds a value, such as a number, a character, or a piece of text. The value stored in a variable can change while the program is running, which is why it is called a variable.

Whenever you want to store information like a student’s marks, an employee’s salary, or a user’s name, you use a variable. Without variables, a program would not be able to process or remember data.

C++ Variable Declaration

Before using a variable, you must declare it. A declaration tells the compiler the type of data the variable will store and the name you want to give it.

Syntax

data_type variable_name;

Example

int age;
float salary;
char grade;

Here:

  • int stores whole numbers.
  • float stores decimal numbers.
  • char stores a single character.
  • age, salary, and grade are variable names.

At this stage, the variables are declared but they do not have any values.

Initializing a Variable

You can also assign a value to a variable at the time of declaration. This process is called initialization.

Example

int age = 20;
float salary = 45000.50;
char grade = 'A';

In this example, each variable is declared and given an initial value.

C++ Variables with Examples

The following program shows how variables are used in a C++ program.

Example

#include <iostream>
using namespace std;

int main() {
    string name = "Rahul";
    int age = 21;

    cout << "Name: " << name << endl;
    cout << "Age: " << age;

    return 0;
}

Output

Name: Rahul
Age: 21

In this program:

  • name stores the student’s name.
  • age stores the student’s age.
  • cout displays the values stored in the variables.

Rules for Naming Variables

When choosing a variable name, follow these simple rules:

  • Start the name with a letter or an underscore (_).
  • Use only letters, digits, and underscores.
  • Do not start a variable name with a number.
  • Do not use spaces or special characters.
  • Avoid using C++ keywords as variable names.
  • Choose meaningful names that describe the stored data.

For example, studentName is much better than x because it clearly tells you what the variable stores.

Key Points

  • Variables are used to store data in a program.
  • Every variable must have a data type and a name.
  • A variable should be declared before it is used.
  • You can assign a value during declaration or later in the program.
  • Meaningful variable names make your code easier to read and maintain.

Frequently Asked Questions (FAQs)

Q1. What are variables in C++?

Variables in C++ are named memory locations used to store data. Their values can be changed during program execution.

Q2. How do you declare a variable in C++?

A variable is declared by specifying its data type followed by its name. For example: int age;.

Q3. What is the difference between variable declaration and initialization in C++?

Declaration creates the variable, while initialization assigns it an initial value. For example, int age = 20; declares and initializes the variable at the same time.

Q4. What are the rules for naming variables in C++?

Variable names must start with a letter or underscore, can contain letters, digits, and underscores, cannot contain spaces or special characters, and cannot be C++ keywords.

Q5. Can the value of a variable change in C++?

Yes. Variables are designed to store values that can be updated during program execution, unless they are declared as const.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top