Open In App

C++ Variables

Last Updated : 10 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution.

Creating a Variable

Creating a variable and giving it a name is called variable definition (sometimes called variable declaration). The syntax of variable definition is:

type name;

where, type is the type of data that a variable can store, and name is the name assigned to the variable. Multiple variables of the same type can be defined as:

type name1, name2, name3 ....;

The data type of a variable is selected from the list of data types supported by C++.

Example:

To store number without decimal point, we use integer data type.

int num;

Here, int is the keyword used to tell the compiler that the variable with name num will store integer values. The C++ Course covers the types of variables in C++ and how to use them effectively in your programs.

Initializing

A variable that is just defined may not contain some valid value. We have to initialize it to some valid initial value. It is done by using an assignment operator = as shown:

type name;
name = value;

Definition and initialization can also be done in a single step as shown:

type name = value;

where the value should be of the same type as variable.

Example:

int num = 100;

The integer variable num is initialized with the value 100. Values are generally the literals of the same type.

Accessing

The main objective of a variable is to store the data so that it can be retrieved later. It can be done by simply using its assigned name.

Example:


    // Creating a single character variable
    char c = 'a';
    
    // Accessing and printing above variable
    cout << c;


Output
a

Updating

The value stored in the variables can be changed any number of times by simply assigning a new value using = assignment operator.

Example:


    int num = 24;
    cout << num << endl;
    
    // Assigning new value
    num = 888;


Output
24
888

Rules For Naming Variable

The names given to a variable are called identifiers. There are some rules for creating these identifiers (names):

  • A name can only contain letters (A-Z or a-z), digits (0-9), and underscores (_).
  • It should start with a letter or an underscore only.
  • It is case sensitive.
  • The name of the variable should not contain any whitespace and special characters (i.e. #, $, %, *, etc).
  • We cannot used C++ keyword (e.g. float, double, class) as a variable name.

How are variables used?

Variables are the names given to the memory location which stores some value. These names can be used in any place where the value it stores can be used. For example, we assign values of the same type to variables. But instead of these values, we can also use variables that store these values.


    int num1 = 10, num2;
    
    // Assigning num1's value to num2
    num2 = num1;
    cout << num1 << " " << num2;


Output
10 10

Addition of two integers can be done in C++ using + operator as shown:


    cout << 10 + 20;


Output
30

We can do the above operation using the variables that store these two values.


    int num1 = 10, num2 = 20;
    cout << num1 + num2;


Output
30

Constant Variables

In C++, a constant variable is one whose value cannot be changed after it is initialized. This is done using the const keyword.

#include <iostream>
using namespace std;

int main() {
    const int num = 10;
    cout << num;
    return 0;
}

Scope of Variables

Scope of variable is the region inside the program where the variable can be referred to by using its name. Basically, it is the part of the program where the variable exists. Proper understanding of this concept requires the understanding of other concepts such as functions, blocks, etc.

Memory Management of Variables

When we create or declare a variable, a fixed-size memory block is assigned to the variable, and its initial value is a garbage value. Initialization assigns a meaningful value using the assignment operator. Variables essentially manipulate specific memory locations, and their stored data is accessed via their names.

Moreover, different variables may be stored in different section of memory according to its storage class.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg