C++ Variable Scope

    In this C++ tutorial, we will discuss the Variable Scope. We will also discuss the various type of variable scope used in C++, using the appropriate examples.

    Variable Scope

    The scope of a variable defines the visibility area of that variable in the program. In C++ we can have different make different functions where the main() function has the priority, and it compiles & executes first. In simple words, we can say that the scope of a variable is a concept that defines, which block of function is capable of accessing that variable.

    Type of Variables Scope

    In C++ we generally have two types of variable scopes:

    • Local Scope Variable
    • Global Scope Variable

    1. Local Variable

    A Local Scope Variable also refers as the local variable. A variable which is defined or declared inside a block of a function is known as Local Variable. Only the function where the local variable is defined is able to access the variable. Local Variable Example 1:

    #include <iostream>
    using namespace std;
    int main()
        {
                int x = 20;              // x is a local variabel
                cout<<x;
        }

    Output:

    20

    Behind the Code: In the above code, we define a variable x =20; where x is a local variable because it is defined inside the main() function, and only main() function would be able to access this variable. Local Variable Example 2: “What if we try to access a local variable of another function”

    #include <iostream>
    using namespace std;
    
    int fun()
    {
       int x= 80;   // x is the local variable of fun()
    }
    int main()
       {
           cout<<x;   // main function accessing local variable of function fun()
       }

    Output:

    [Error] 'x' was not declared in this scope

    Behind the Code: In the above code we have two functions fun() and main(), the variable x =80; is defined and declared in function fun() and only function fun() is able to access x variable, so when we try to print the value of x in main() function cout<<x; then it throw an error “'x' was not declared in this scope”

    2. Global Variable

    A Global variable is just the opposite of Local Variable. If a variable is defined or declared outside of all the functions, then it would be considered as a global variable. The visibility of a global variable remains throughout the program and any function is capable of accessing that variable. Example:

    #include <iostream>
    using namespace std;
    
    int x= 20;           //x is a globale variable
    
    int main()
       {
          cout<<x; // main function accessing global variable
       }

    Output:

    20

    Behind the Code: Here the variable x=20; is defined outside the main function, which make x a global variable that why the main() function itself able to access the variable x.

    What is the global and local variable shear the same name in C++?

    This is a very valid question and in interviews, its asked very often,” what happens if the global and local variable shear the same name would it cause an error if not then which variable the function give priority?”.

    • No, it won’t throw any error, if a global variable and local variable shear the same name, then the compiler uses the concept of namespace and distinguish each variable on the basis of their scope and treat them both as two different variables.
    • If the local and global variable shear the same name, then the function give priority to the local variable. If we try to access a variable inside the function block, the compiler first looks for the local variable if the variable is not inside the function block then it searches for the global variable.

    Example:

    #include <iostream>
    using namespace std;
    int x= 200; //x is a globale variable
    int main()
       {
            int x = 100; //local variable x
            cout<<x;
       }

    Output:

    100

    Behind the code: In this example we have defined 2 variable with the same name with different data value int x=200 and int x = 100;. Here x with 200 value is a global variable and x with 100 value is a local variable. So, when we print out the value of x inside the main() function it gives an output of 100 which was a local variable. This signify that function give priority to the local variable.

    Scope Resolution Operator

    In the above example, we show that, if the local and global variables shear the same name than the function give priority to the local variable. So what if we want to access the global variable, and we also have the local variable with the same name. For this we use the Scope Resolution Operator :: A scope resolution operator helps the function to access the global variable if the function has the same-named local variable. The sign of the scope resolution operator is two consecutive colons :: Example :

    #include <iostream>
    using namespace std;
    
    int x= 200; //x is a globale variable
    
    int main()
         {
              int x = 100;
              cout<<"The value of the local x is " <<x;       // accessing local x
              cout<<endl;                                     //endl is used to give a new line space
              cout<<"The value of the Global x is " <<::x;    //using :: sign to access global x
        }

    Output:

    The value of the local x is 100
    The value of the Global x is 200

    Behind the code: In this example we use the scope resolution operator before variable ::x to access the global variable. Note: the statement cout<<endl; used to print the new line in the output.

    Scope Variable Quick Summary:

    • The scope of the variable defines the visibility area of that variable in the program.
    • The scope of the variable simply defines whether or not we can access a variable wherever we want.
    • There are two types of scope variable in C++ Local variable and Global variable
    • If the variable is defined inside a function block, then it would be considered as a local variable.
    • If the variable is defined outside all the functions block, then it would be considered as a global variable.
    • If the local and global variable shear the same name, then the compiler give priority to the local variable.
    • We use the Scope Resolution Operator to access the global variable inside the function if the function has a variable same name as a global variable.

    People are also reading: