Variable Scope in C++

Variable Scope in C++: A variable scope in C++ is nothing but the scope of a variable can be defined as the extent of the programming code in which the code can be accessed (or) declared (or) worked. Variables can be declared in multiple ways each with different memory requirements and functioning.



Variable Scope in C++

In C++ we are having two different types of variables. They are as follows.

  • Local variables
  • Global Variables

Local Variables

The variables which are declared within a function (or) a block are said to be “local variables“. These variables can be used only by the statements that are inside of the block (or) functions.

  • These local variables are not known to the functions outside by their self.
  • Anything which is present between ‘{‘  and ‘}’ are said to be inside a block.

Local Variables Example:

// usage of local variables  
#include<iostream> 
using namespace std;  
void func() 
{    
//this variable is local to the 
//function func() and cannot be  
//accessed outside this function 
int age=18;  
cout<<age; 
} 
int main() 
{ 
    cout<<"Age is: "; 
    func();       
    return 0; 
}

Output:

Age is:18

Global Variables

Global variables can be declared from any part of the program. These variables hold the value throughout the life time of the program. And these variables can be accessed by any of the function. They must be declared outside the main() function

Global Variables Example:

#include<iostream> 
using namespace std; 
// global variable 
int global = 5; 
// global variable accessed from 
// within a function 
void display() 
{ 
    cout<<global<<endl; 
} 
// main function 
int main() 
{ 
    display();      
    // changing value of global 
    // variable from main function 
    global = 10; 
    display(); 
}

Output:

5
10

Suppose if a program has the same name for both global and local variables. so, in this case, the value of the local variable inside a function will take the preference.

Example:

#include<iostream> 
using namespace std; 
// global variable 
int global = 5;   
// main function 
int main() 
{    
//local variable with same  
//name as that of global variable 
      
int global = 2; 
cout << global << endl; 
}

Output:

2

From the above example, we can see that same name for both global and local variables.

  • when two variables with the same name are defined then the compiler produces compile time error.
  • In case if the variables are defined in a different scope, the compiler allows.
  • So, in this case, the compiler will give preference to the local variable.

How to access global variables when there is a local variable with the same name?

So, in this case, we should use the scope resolution operator.

Scope Resolution Operator

It can be used to define a function outside the class (or) when we want to use a global variable, but also have the local variable.

Example:

#include<iostream>  
using namespace std;   
// Global x   
int x = 0;   
int main() 
{ 
  // Local x     
  int x = 10;  
  cout << "Value of global x is " << ::x; 
  cout<< "\nValue of local x is " << x;   
  return 0; 
}

Output:

Value of global x is 0
Value of local x is 10

Example: Scope resolution operator in Class

#include<iostream>
using namespace std;
class programming {
public:
  void output();//function declaration
}; 
//function definition outside the class 
void programming::output() {
  cout<<"Function defined outside the class.\n";
}
int main() {
  programming x;
  x.output();
  return 0;
}

Output:

Function defined outside the class.