'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Last Updated: Jul 2, 2023

Local and Global Variables

Author Vivek Goswami
0 upvote
gp-icon
Basics of C++
Free guided path
9 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction 

This blog is concerned with the declaration of variables in C++. Variables store data in a particular memory location, later modified and operated on in the program. Before using the variables in the program, we need to declare them. 

The syntax for variable declaration is:

 For one variable:

Variable_type variable_name;

For more than one variable of a single type:

Variable_type variable_name1, variable_name2, variable_name3;

Variable are of two types based on their declaration in the program:

  1. Global Variable 
  2. Local Variable


Let us dive deeper into the two variables and learn some new concepts.


Also, see Literals in C. Fibonacci Series in C++

Scope of a Variable

Before beginning to understand the local and global variables, it is essential to understand the concept of the scope of a variable. The variable's scope defines where the variable would be accessible for use in the program. 

The primary difference between global and local variables lies in the scope of the two variables, which further depends on how the two variables are defined.

Read More About, C++ Basics

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Local Variables

Local variables are defined within a particular program block, i.e., inside a function. They are accessible for use only inside that specific function or in the block those variables are defined. Outside the block, we cant use the variables.

Code for the declaration of local variables is as follows:

#include<bits/stdc++.h>
using namespace std;
int main()
{
  int p, q, r;        // Declaring local variables p,q and r.
  p=5;               // Initialising value of local variable p.
  q=9;               // Initialising value of local variable q.
  r= p*q;        // Multiplying them and declaring r with the value.
  cout<<r;
  return 0;
}


Output:

45

Here, the variables p,q, and r are declared inside the function - void main() in this code block. Hence, they can be used only inside the function in which they have been defined. However, we can't use these variables outside the function if another function is present in the program.

Global Variables

As the name suggests, we can access global variables throughout the program. Any function in the program can access these variables and use them.  

Following is the program illustrating the use of a global variable in C++:

#include <iostream>
using namespace std;
int p = 15;
// Declaring global variable p and initialising it with value 15.

int main()
{
    int r, s;  // Declaring local variables r and s.
    r = 65;    // Initializing value of local variable r.
    s = p * r; // Initializing s as p*r.
    cout << s;
    return 0;
}


Output:

975


As you would have noticed in the above program, the variable p is declared outside the code block void main(); however, you can still access it by the block for various operations. The variable's scope is global, which implies that it can be used anywhere in the program by any function. The variables can be accessed and updated anywhere in the program. 

Another observation that we need to make is that, unlike local variables, these variables are not defined within a particular code block. They are defined separately in the program.

You can practice by yourself with the help of online c++ compiler. You can also do a free certification of Basics of C++.

Also Read - C++ Interview Questions

Using Local and Global Variables

We generally use both local and global variables for our convenience in a program that we write. 

So, let us look into a program that has both local and global variables.

#include <iostream>
#include <conio.h>
using namespace std;

int p = 76; // Declaring global variable p and initializing it with value 76.

int main()
{
    int r, s, t; // Declaring local variable r,s and t.
    r = 5;       // Initialising value of local variable r.
    t = 78;      // Initialising value of local variable t.
    s = r + t;   // Initialising value of local variable s as sum of r and t.
    s = s * p;   // Initialising value of local variable s as a product of s and p.
    cout << s;
    return 0;
}


Output:

6308

 
In the above program, both the concepts of global and local variables are used. The global variable is p, whereas the local variables are r, s, and t. 

Also see, Abstract Data Types in C++

Variables Having Same Name

Another interesting situation in the declaration of these variables arises when the name of global and the local variables are of the same name. 

What would the result be in such a case?

Let us look into it and understand the problem:

First of all, if the name of any two local variables is the same, then the compiler produces a run time error because it has no idea which memory location to refer to and use. 

Secondly, if a local and global variable is of the same name, the compiler uses the precedence as per the language. In C++, precedence is given to the local variable of that particular blog, and hence, its value is used, updated, and displayed in the program results. It must be taken care of while using two variables of the same name with different scope. However, it is advised to usually use two different names to avoid confusion in the program.

Another question that might pop into your mind next is if the name of the global variable and local variable is the same and the local variable is used when defined in a code block, how could you access and use the global variable if you want to. 

To understand the solution to this problem, let us look into the following code:

#include <iostream>
using namespace std;
int p = 45;
int main()
{
    int p = 54;
    cout << "the global variable is " << ::p << endl;
    cout << "the local variable is " << p;
    return 0;
}


Output:

the global variable is 45
the local variable is 54


Concerning the first cout statement, we get the value of the global variable p, which is 45, and in another cout statement, we get the value of the local variable p, which is 54.

Frequently Asked Questions

  1. Differentiate between global and local variables in terms of scope.
    The main difference between global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only in the block or function they are declared. 
     
  2. How can we use local variables as global variables?
    By using const or static keywords before the declaration.
     
  3. Is there any difference in the declaration of int in the global and local variables?
    Yes, because when variables are defined globally, int has an initial value of 0 or one bit.
    Whereas when declared locally, it has an 8-bit initial value, thus it takes more time in the definition of local variable.

Key Takeaways

We learned how to declare global and local variables in this blog. This blog also discussed the use of these variables in the program. Further, the condition where the two variables' names are the same was discussed in the blog, along with code segments for each case.

You can learn more about such programming concepts here.

Previous article
Constants/Literals
Next article
C++ Data Types
Guided path
Free
gridgp-icon
Basics of C++
9 chapters
104+ Problems
gp-badge
Earn badges and level up
Live masterclass