C++ Global Variable

C++ Global Variable
Techiio-author
Written by Sagar RabidasJanuary 28, 2022
8 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Today we will discuss C++ Global Variable

C++ Global Variable

In c++, a global variable is described as a variable that may be used or accessed from anywhere within the complete software, which is one of the scope sorts in any programming language. In which the global variable scope is the quantity of this system code within which the variables may be accessed or described or declared, or used, in general, the global variable is defined as a variable this is allowed to be utilized by any part of this system with none restrict or errors and are to be had to any part of the program or all through the entire application, but they may be declared or described generally at the pinnacle or beginning of the program which could be out of doors of all the blocks and capabilities of this system.

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:

blogpost

In the above application, we can see we've got declared and described worldwide variable “g” with a cost as 20 outside the principle() function and neighborhood variable “p” with a fee of 10 within the application. So while we're printing a local variable, we can see we will print or use it inside the feature, however, the worldwide variable cost may be changed even inside the characteristic in which on this above code, we are incrementing the g fee using 1. So the output could be “21” for the value of the worldwide variable. So within the above screenshot, we can see the output.

In c++, the variables are classified into worldwide, local, static, and many others., based on the garage class of variables. So the variables that are defined outdoor all of the capabilities and blocks however inside the application are referred to as global variables. In c++, there might be a situation in which each global and neighborhood variable has the identical call that's puzzling to the compiler and might throw an error, but if there are in specific scopes, then the compiler will print the value of the neighborhood variable name’s price because it gives the first choice to neighborhood variables than global.

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:
blogpost

Inside the above application, we can see we have each global and nearby variable with the equal name as “g”, however, whilst we're seeking to print each the values, it will print “nine. Three” only for each because the compiler will supply greater preference to a nearby variable over a worldwide variable. Therefore, within the above screenshot, the output gives 9.3 most effective while we're printing each global and nearby variable value. So to print the worldwide variable price, we need to use scope resolution operator so the above code might be modified inline eleven wherein while we are printing g fee, we should write it as::g to get entry to the price of gas a worldwide variable which we can see in the below with the modified code and output showing the values of each international 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:
blogpost

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 can print both values of a global variable using scope resolution operator (::) in line 11 and local variables in line 13.

C
C plus plus
Global Variable
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance