Scope of variables in C++

This tutorial is about the scope of variables in C++. The scope of a variable is the area in the program code in which it can be used or accessed. Out of this area, the variable has no existence and hence it cannot be called or used. We will learn about the scopes of different types of variables in this tutorial.

Learn Scope of variables in C++

A variable can be classified in the following categories based on their scopes.

  • Local variables
  • Global variables

Let’s understand these variables one by one.

Local Variables

A local variable is a variable that is declared inside a function or a block. They can be used only by the statements inside the block or the function they are declared within. A block in C++ is area between ‘{‘ and ‘}’.

See the example code for better understanding.

#include <iostream>
using namespace std;
void fun();

void fun()
{
  //declaring and initializing  variable a inside the function fun()
  int a=5;
  cout<<"value of a is: "<<a;
}

int main()
{
  //calling function fun()
  fun();
  return 0;
}

Output:

value of a is: 5

Now see the next example.

 

#include <iostream>
using namespace std;
void fun();

void fun()
{
  //declaring and initializing variable a
  int a=5;
  
}

int main()
{
  fun();
  // accessing a from main()
  cout<<"value of a is: "<<a;
  return 0;
}

Output:

Error: 'a' was not declared in this scope.

Here, as you can see the compiler throws an error because we have tried to a variable whose scope is limited to the function fun(). In the same way, if we try to use a variable outside of a block it is declared in, the compiler will throw an error. See this code.

#include <iostream>
using namespace std;

int main()
{
  //A block
  {
    int a=3;
  }
  cout<<"value of a is: "<<a; //error
  return 0;
}

Output:

Error: 'a' was not declared in this scope.

Global Variables in C++

Global variables are the variables whose scope is not limited to a block or function. They exist throughout the whole program and can be used in any part of the program. To use a variable as a global variable you must declare it outside of all the blocks or functions. They are usually declared at the top of the program as shown in the example code. However,  that is not mandatory. See the example code.

#include <iostream>
using namespace std;
void fun();

int a;

void fun()
{
  a++;
}

int main()
{
  //changing value of a
  a=3;
  //incrementing a
  fun();
  cout<<"value of a is: "<<a;
  return 0;
}

Output:

value of a is: 4

Local and global variable with the same name in C++

If we declare a global variable and a local variable with the same name, the compiler will give precedence to the local variable i.e. in a region where both local and global variables of the same name exist, all the operation done on the variable will be done on the local variable. See this example.

#include <iostream>
using namespace std;

//global variable
int a=4;

int main()
{
  //local variable
  int a=9;
  a++;
  cout<<"value of a is: "<<a;
  return 0;
}

Output:

value of a is: 10

However, we can access the global variable using scope resolution operator. See here.

#include <iostream>
using namespace std;

//global variable
int a=4;

int main()
{
  //local variable
  int a=9;
  a++;
  cout<<"value of local variable a is: "<<a<<endl;
  cout<<"value of global variable a is: "<<::a;
  return 0;
}

Output:

value of local variable a is: 10
value of global variable a is: 4

Thank you.

Read also:

Leave a Reply

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