C++ variables scope:local variables and global variables

In this post we will see what does local variable and global variable means and also we will discuss when to use scope resolution operator and what is it’s function

Variables scope in C or C++ mean the region under which a particular variable is alive or it can be utilized.The variable scope is mainly define by where it is declared in a program.It can be inside a function or it can be outside the function.Sometime it can be inside a class or structure in a header file.Depending on where it is declared variables scope is divided into two types:

i)Local variables scope and
ii)Global variable

Link :What is variable?


Local variables

A variable is a local variable or it is said to have a local scope if it is declared locally.A region is considered as local region if it is found under a braces or the region is own by a function.

i))Region define by a braces ( ‘{}’ )
i)Region own by a function(Read this only after you have known what is function)

i)Region define by a braces

Whenever we use braces(‘{}’ ) in our program,the region under the braces is considered as a local region.Consider the variable ‘loc’ declared inside the braces.



int main( )
{
int i=90;

  { /**opening braces define here so local region starts here **/

  int loc=89 ;

  cout<< i << endl
  << i ;

  }/**Closing braces define here ending of local region **/

cout<< i << endl //work fine
 << loc ; //error!!

return 0;
}

The variable ‘loc’ cannot be accessed outside the braces because it exist only inside the braces region.So local variable have a life time only under the local region.Here is another example.

int main( )
{
int i=90;

   { //local region 1
      int loc1=2;
       
      { //local region 2
         int loc2=5;
         cout<< loc1 << endl 
             << loc2 ; // work fine
      
      } //local region 2 ends
      
      cout<< loc2 ; //error!
   
    } //local region 1 ends
   
 cout<< loc1 ; //error!!   

cin.get();
return 0;
}

In the program above ‘local region 2’ is define under ‘local region 1’.So,the ‘loc1’ variable has a life time under local region 1 and local region 2 but ‘loc2’ variable has a life time under only local region 2.

Naming a region as done in the program above is a good practice as it create a fine distinction between various local regions and the variables that belong to them.


ii)Region own by a function

Whenever a function is defined it owns a certain region.The region it owns will contain the code that will be executed whenever the function is called.If any variables is declared inside the function it will be taken as local variable.The reason is simple,any variable declared inside the function cannot be accessed outside the function.Consider the function given below.

void func( )
{
int var=90;

cout<< var_main ; //error
}


int main( )
{
int var_main=89;

cout<< var ; //error

return 0;
}

The variable of function func() ‘var’ is inaccessible inside the main() function as it has a local scope and has a life time only under the region own by the function func().Note main() is also a function so the variable ‘var_main’ cannot be accessed inside func().So ‘var_main’ also has a local scope.

 

Inside a function we can again have a local scope surrounded by braces.One instance of this case is also discussed above; local region inside main( ).Here is another example.

void func( )
{
int var=90 ;

  {//Local scope 1 begins

  int var1=930 ; //exist only inside local scope 1

  }//local scope 1 ends

cout<< var1 ; //error
}

the code ‘cout << var1’ and I am sure you know why?

If we look logically the concept of local scope of a variable is a relative term.A scope is a local scope if it occupy a smaller region inside another scope.The number of local scope inside another local scope can be infinite and always lies under the influence of another larger (local or global) scope.So we always compare a smaller scope with a larger scope to come to the conclusion that it is a local scope.


Global variables

A global variable has an influence over the entire region of a particular file(can be .cpp or .h) or program.By this we mean a variable having a global scope can be accessed anywhere inside the file or program.

Whether it is inside a function or may be inside another local scope,the global variable can be used.To make a variable a global variable we declare the variable outside all the braces and scope limiting element.The scope limiting element here refer to all the functions including main().A simple program is given below.

int glo=90; //declared outside main()

int main()
{
int i=9;

cout << i << endl 
<< glo << endl ; //work fine

{ //local region 1

cout<< glo ; //work fine

} //local region 1 end

return 0;
}

The ‘glo’ variable declared outside the main() is a global variable and it can be accessed anywhere inside the main() region or even inside the local region 1.

Another example using function is given below.

#include<iostream>

using namespace std;

int glo=657 ; //'glo' is a global variable

void func( )
{
cout<< glo ; //work fine 
}

int main( )
{
int i=90 ;

cout<< glo ;//work fine

 {
 cout<< glo ;//work fine
 cout<< i ;//work fine
 }

return 0;
}

Here you can see that the variable ‘glo’ can be accessed from anywhere whether it may be inside func() function or main() function.This is the power of global variable.


Global variable and local variable with the same name:use of scope resolution operator

What happen if the global variable and a local variable have the same name and note C++ allow giving the same name to a global and local variable.

If a global and local variable have the same name and if we try to access the variable then note the local scope variable with the same name has a greater influence in that local region and so it will be used.However,if the local variable cease to exist then the global variable will be used instead.Consider the example below.

int val=890 ; //GLobal variable

void func( )
{
int val=555;

cout<< val; //gives 555,local variable is used
}

int main( )
{
 { //local region 1

 int val=12 ;

 cout<< val << endl ; //gives 12 ,local variable is used here not global variable
 
}//local region ends

cout<< val; //gives 890 ,global variable is used

return 0;
}

Output,

12
890

 

Inside the ‘local region 1’ accessing ‘val’ gives 12 meaning the local variable is used.Outside the local region the accessing ‘val’ gives 890 obviously the global variable is used.So we can conclude that if global and local variable has the same name then the local variable is given higher priority in that local region.

What if we want to use the global variable inside a local region but a local variable with the same name exist.We can do so using an operator known as scope resolution operator(::).Using this operator we can signal the compiler that we want to use the global variable not the local variable.A simple program is shown below.

int value=890;

void func( )
{
int val=555;

cout<< "Val=" << val endl ; ///gives 555
cout<< "::val=" << ::val << endl ; //gives 890,global variable used
}

int main( )
{
 {//local region
 int val=12 ;
 
 cout<<"\nInside local region \n\n" ;
 cout<< val ; //gives 12 ,local variable is used
 cout<< ::val ; //gives 890 ,global variable used

 } //local region ends

cout<< "\n\n Outside local region\n";
cout<< val << endl //gives 890
<< ::val ; //still gives 890

return 0;
}

Output,

val=555
::val=890

Inside local region
val=12
::val=890

Outside local region
val=890
::val=890

Using the scope resolution operator is the best way to distinguished between a global and local variable.It is also easy to use and remember.


Conclusion

The variables scope:local variable or global variables follows the rule discuss above in a particular file.If you have another file you cannot access the local variables(this goes without saying) and you cannot also access the global variable of another file.If you want to make a global variable of another file available in that file we will employ a different method.The discussion of this method is done in another post.If you have any doubts comment below or contact us.Happy programming!




Leave a Reply

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