Group Discounts available for 3+ students and Corporate Clients

Types of variables in C++

C++ Local Variables

The variable declared inside function is called local variable in C++

  • Memory for this variable is allocated on execution of function and after completion of function
  • The scope of this variable within function
  • Local variable are called auto variable.
  • Auto is storage class and variable is never declared without class
  • Storage class define scope life time and storage of variable.



void main()


int x;

int Y;

int Z;

Cout << X << endl;



o/p : The output of above program is garbage value, Because local variables are not given any default values.

  • C++ allows to declare local variable anywhere within function


# include <iostream.h>

void main()


int n1;

cout<< “\n input n1 value”;

cin >> n1;

int n2;

cout<< “\n input n2 value”;

cin>> n2;

int n3;


cout<< “\n sum is ” << n3



void main()


  • C++ allows to declare block level variables.

C++ Block level variables

The variables which are declared inside the anyone of the following block is called as Block level variables in C++

  1. Anonymous block
  2. Conditional block
  3. Iterational block




int x;    – > block level


Conditional Block



int y;          Block level


Iterational block


# include <iostream.h

void main()


char name[10];

int sub1,sub2;

cout<<”\n input name”;

cin>> name;

cout<< “\n input two subjects”;

cin>> sub1 >> sub2;

cout<< “\n input two subjects”;

cin>> sub 1>> sub 2;

cout << “\n name” << name;

if(sub1>=40 && sub2 >=40)


int total = sub1+sub2;

flat avg=total/3;

cout<<”\n Total” << total;

cout<< “\n Avg” << avg;

cout<< “\n Result is pass”;



cout<< “\n result fail”


  • C++ allows to declare local and block level variables with same name
  • This is called as hicling



void main ()


int x=10;

int y=20;

cout<<”\n local variable x=”<<x    10

cout<<”\n local variable y=”<<y; 20


int z=30;

cout<< “\n Block local variable z=” <<Z; 30

int x=40;

cout<<”\n Block local variable=” <<x;

cout<<”\n block variable y= <<y”;



  • we can’t access local variable within Block


 C++ Global Variables

  • The variable declare outside the function is called global variable in C++
  • Variable is declared at global to share data between one or more than one function
  • These are called external variables.
  • Extern is a storage class

Syntax :

int x; – > global variable
void main()
int y; – > local variable
int z; block level variable




  • global variables memory is allocated on loading of the program.
  • global variables are given default value
  • if global variable is int, double, float then default value is zero.
  • c++ allows to declare local and global variable with same name.
  • used in c++ 1) ::  (scope resolution)
  • pointers belongs to c++ 2) * (pointer to member access)

3  *(pointer to pointer member access


  • Why we used scope resolution operator ?
  • It is a binary operator and it also work as unary operator

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at, we will update the article in 24 hours.”

0 Responses on Types of variables in C++"

Leave a Message

Your email address will not be published. Required fields are marked *


Please Enter Your Details and Query.
Three + 6