Scope of Variables

Scope of a variable means the extent within a program to which a variable can we accessed or declared or modified.
There are two types of scopes in C++.

Variables with local scope (Local variables)

Variables defined within a function or block are said to be local to those functions.
Anything between ‘{’ and ‘}’ is said to inside a block.
Local variables do not exist outside the block in which they are declared, i.e.
they cannot be accessed or used outside that block.
A simple program illustrating local variables

#include<iostream>
using namespace std;
 
void foo()
{  
    int x = 18;    
}
 
int main()
{
    cout<<"x =  "<<x;
     
    return 0;
}

Output: Error: x was not declared in this scope

The variable ‘x’ was declared within the function foo() so it is local to that function and not visible to portion of program outside this function.
As we are accessing ‘x’ inside main() and there is no ‘x’ inside main(), it is only in foo() block.
Therefore the compiler throws an error.

To use ‘x’ inside main(), either define a variable ‘x’ inside main() or call foo() from main() function.

Variables with Global Scope (Global Variables)

Global Variables can be accessed from any part of the program.
They are available throughout the life time of a program.
They are declared outside all functions and blocks (generally at the top of program) and therefore are accessible in all blocks i.e.
throughout the program code.
A simple program illustrating global variables

#include<iostream>
using namespace std;
 
// global variable
int x = 15;
 
void foo()
{
    cout<<x<<endl;
}
 
// main function
int main()
{
    foo();
    x = 10;
    foo();
}

Output is:

15
10

In the program, the variable “x” is declared at the top of the program outside all of the functions so it is a global variable and can be accessed or updated from anywhere in the program.

What if there exists a local variable with the same name as that of global variable inside a function?

Let’s look at the program below,

#include<iostream>
using namespace std;
 
// global variable
int x = 5;
 
// main function
int main()
{        
    int x = 2;
    cout << x << endl;
}

As we know that declaring two variables with same name is not allowed, doing this compiler throws an error.
But when two different variables with same name have different scope, compiler allows it.
When there is a local variable inside the block with same name as any one of global variables, the local variable hides the global variable inside that block.
In the above program the output is 2 as the local ‘x’ hides the global ‘x’.
Than how to access the global variable inside the block which has a local variable with same name? To solve this problem we will need to use the scope resolution operator (::).
Below program explains this thing,

#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 of the above program is,

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

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?