Updated April 17, 2023
Definition of C++ Global Variable
In C++, a global variable is defined as a variable that can be used or accessed from anywhere in the entire program, which is one of the scope types in any programming language. Where the global variable scope is the extent of the program code within which the variables can be accessed or defined or declared, or used, in general, the global variable is defined as a variable that is allowed to be used by any part of the program without any restriction or error and are available to any part of the program or throughout the entire program, but they can be declared or defined usually at the top or start of the program which would be outside of all the blocks and functions of the program.
Working of Global Variable in C++
In this article, we will discuss a global variable in C++. There is a concept in C++ known as scope of variables in the programs, which means the variable’s accessibility within the program, within the function or block in the program. Therefore there are 2 types of scope variables in C++, such as local variables that are the variables declared within the block or function of the program and are only available to those blocks and functions of the program. Another type of scope variable is global variables that are the variables declared or defined in the beginning or at the top of the program and are available to the entire program such that they can be accessed through any part of the program. So, in general, it means that the global variables can be defined at the top of the program and can be seen and modified by any functions (including main) or blocks in the program that are referencing this global variable.
Examples of C++ Global Variable
Let us see in the below example how to define and declare such global variables in C++ programs.
Example #1
Code:
#include <iostream>
using namespace std;
float g = 20;
int main () {
std::cout<<"A simple program for demonstrating global variables in C++"<<endl;
std::cout<<"\n"<<endl;
float p;
p = 10;
std::cout << "The value of the local variable p is " << p <<endl;
g++;
std::cout<<"\n"<<endl;
std::cout << "The value of the global variable g is " <<g <<endl;
return 0;
}
Output:
In the above program, we can see we have declared and defined global variable “g” with a value as 20 outside the main() function and local variable “p” with a value as 10 within the program. So when we are printing a local variable, we can see we can print or use it within the function, but the global variable value can be modified even within the function wherein in this above code, we are incrementing the g value by 1. So the output will be “21” for the value of the global variable. So in the above screenshot, we can see the output.
In C++, the variables are classified into global, local, static, etc., based on the storage class of variables. So the variables which are defined outside all the functions and blocks but within the program are known as global variables. In C++, there might be a situation where both global and local variable has the same name which is confusing to the compiler and may throw an error, but if there are in different scopes, then the compiler will print the value of the local variable name’s value as it gives first preference to local variables than global.
Hence sometimes global variables are considered as dangerous to use but as this one of the best way to have accessibility to any of the functions or blocks in the entire program which is very lengthy and it’s useful when it is difficult to declare the variables each time in the functions or blocks. So in such cases, to get access to global variables that have the same name as that of the local variable, then C++ provides an operator known as scope resolution operator (::). So to understand this, we will consider a simple example below.
Example #2
Code:
#include<iostream>
using namespace std;
float g = 5.8;
int main()
{
float g = 9.3;
cout << "This program uses scope resolution operator to access global variable value " << endl;
cout <<"\n" <<endl;
cout << "The value stored in the global variable g is " << g <<endl;
cout <<"\n" << endl;
cout<< "The value stored in the local variable g is " << g <<endl;
return 0;
}
Output:
In the above program, we can see we have both global and local variables with the same name as “g”, but when we are trying to print both the values, it will print “9.3” only for both as the compiler will give more preference to a local variable over a global variable. Therefore, in the above screenshot, the output gives 9.3 only when we are printing both global and local variable values. So to print the global variable value, we need to use scope resolution operator so the above code will be modified inline 11 where when we are printing g value, we have to write it as::g to access the value of g as a global variable which we can see in the below with the modified code and output showing the values of both global and local variable values.
Code:
#include<iostream>
using namespace std;
float g = 5.8;
int main()
{
float g = 9.3;
cout << "This program uses scope resolution operator to access global variable value " << endl;
cout <<"\n" <<endl;
cout << "The value stored in the global variable g is " << ::g <<endl;
cout <<"\n" << endl;
cout<< "The value stored in the local variable g is " << g <<endl;
return 0;
}
Output:
In the above program, we can see that we have declared g as a global variable at the top of the program before the main() function which holds the “5.8” value. And we have declared local variable g within the main() function, which holds a “9.3” value. So in the above screenshot, we can see we are able to print both values of a global variable using scope resolution operator (::) in line 11 and local variables in line 13.
Conclusion
In this article, we conclude that a global variable in C++ is a variable defined and declared at the top of the program, which can be available to the entire program even within the functions or blocks compared to a local variable. This article saw how we can declare global variables and use them in the program with a simple example. In this, we also saw if there is an ambiguity in the names of the variables, which means both local and global variables having the same name, then the global variable can be accessed using the scope resolution operator with an example in the above article.
Recommended Articles
This is a guide to C++ Global Variable. Here we discuss the working of Global Variable in C++ along with the different examples and its code implementation. You may also have a look at the following articles to learn more –