What is variable scope?

Variable scope refers to the area where the influence of the variable is existed. Variable can be declared inside a block or function or outside of a block or function. For this concept, we can divide variable scope in two types.

Two types of variable scope according to their declaration are as follows;

  • Local variable;
  • Global variable;

In this article we will discuss about local variables and global variables in C++. Obviously the concept is same for other programming languages too. This two types of variable is existed according to scope of variable.

C++ local variables & declaration

Local variables are these variables which is declared inside a function or block. The existence of these variables is limited inside that function only. We can not use them outside of that function. But each block inside that function can use a local variable.

Let’s see the example of local variable in C++ bellow;

// using local variable in C++, variable scope

#include <iostream>
using namespace std;

int main(){
    
   int firstNum, secondNum, result;    // local variable for main function only

   firstNum = 35;
   secondNum = 40;
   result = firstNum * secondNum;

   cout << result << endl;

   return 0;
}

In the above program three variable firstNum, secondNum and result are the example of local variable. We can not use them outside the main function.

C++ global variable declaration

Global variable can be used from any function or block of that program. If we declare global variable once, then it is live for all the blocks and functions of our program. Global variables is normally declared outside the main function. We will see an example program in C++ using global variable now.

Let’s see the program bellow;

// using global variable in C++, variable scope

#include <iostream>
using namespace std;

int RADIUS_OF_EARTH = 6400;    // declare global variable

int main(){
    cout << RADIUS_OF_EARTH << endl;   // using global variable

    int fNum = 200, sNum = 300;

    RADIUS_OF_EARTH = fNum + sNum;    // re-assign global variable

    cout << RADIUS_OF_EARTH << endl;

    return 0;
}

Output of this global variable program:

variable-scope-in-c-scope-of-variable-global-variable-local-variable

If you see the output then you may have noticed that the program has printed different value for same variable here. This is happened because the priority of local variable is greater than that of global variable for any block or function.

Initialization of global and local variable

When we declare a global variable, the system assign a default value for that variable like this;

  • int type global variable — 0
  • char type global variable — ‘\0’
  • float type global variable — 0
  • double type global variable — 0
  • pointer type global variable — NULL

But in case of local variable we must have to assign value of that variable. Because the system doesn’t assign any value of a local variable.

However, it is good practice to initialize the value of a variable. Otherwise we will get unexpected result when compile and run our program.

Previous pageVariables in C++

Next page:  Data types in C++