variable and their types in C++

In this page, we will variable and their types in C++ which are following, 

variable in C++

To store user-input in the program, we first declare the variable. The variable type, depend on the user-input.

In a way, you can say that the variable is part of memory location that stores the input given by the user.

Declaration of a variable

The variable-name is an identifier that means any user defined name, as shown below. we declared some variable with different data-types. Which will be used to store different type of information.

int a; int a_,number; float a_b; char name[5];

Here a, a_,number will store the numeric type value and a_b decimal type value while name will store name.

initialization of a variable

When a variable is declared, then at the same time assigning some value or assigning it, is called variable initialization. see example below

int age=25; or int a(25); // for numeric type
char name = 'R'; // for single character
char name [6]= "Rahul"; // for sentence

int and char are data type.

Rvalue – This is the name of variable (e.g. age, name).
Lvalue – This is the value which are assign to the variable (e.g. 25, Rahul).

store name and age, given by user in a program

To store the person’s detail in the program given below, first we declare two variable are name (char type) and age (int type). where name variable used to store person’s name and age variable store person’s age.


 void main()
   int age; 
   char name[6];

   cout<<"Enter age : ";

   cout<<"Enter Name: "

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

Enter age : 25
Enter Name: Rahul

Age :25

The variable declaration in c ++ is done in two ways i.e. you can declare these two types.

  • local variable
  • global variable

The declaration of these variables is the same as the normal variable but by changing the place of declaration, their scope also changes.


local variable in C++

Those variables that are declared within a function or block are called local variables. These types of variable are visible or active within the function. That is when the function executes, only when active, as the function ends, these variables are automatically destroyed. These can not be accessed from outside the function.

Here, l is a local variable which are defined inside the function void main(),

 void main()
   int l=20;

global variable in C++

Those variables that are declared outside a function or block are called Global variables. They can be accessed from any part of the program or from the function.

Here, g is a global variable which are defined outside the function void main(),

int g=10;

 void main()

The program for local and global variables is given below-

Here is the complete program,


int g=10; // global variable declaration

 void main()
    int l=20; // local variable declaration

    cout<<"global variable: "<<g<<endl;
    cout<<"local variable: "<<l;
global variable: 10
local variable: 20

In a large program that contains a lot of functions or blocks, instead of declaring local variable for each function, we use the global variable as per our requirement.

another variable is found in c++, which is not found in c.

reference variable in C++

The reference variable is an alternative to the already defined variable, that is, two such variables whose names are different but their value is same.


Data-type variable-name;
Data-type & reference-variable = variable-name;

In this, the value is assigned in the first variable and in the second variable assigns the first variable. In this way their value is also the same.

let’s see with an example:

In below, v is an int type of variable and r is a reference variable-

int v = 10;
int &r = v;

Here r is the alternative variable of v, where v represented by variable r

cout<<v; // 10
cout<<r; //10



void main()
  int v=10; // variable declared first
  int &r = v; // reference variable declaration

  cout<<"orginal variable: "<<v<<endl;
  cout<<"reference variable: "<<r;

Original variable: 10
reference variable: 10

If the variable name in a program is related to the user-input to be stored in it, then there we can easily identify them and its also helpful to debug the program.
e.g. In a program we declare a char student_name[20] variable. So we can easily identify that the variable student_name is used to store a student name.

previous-Operator and their types

next-Data-type and their types