C++

C++ Global Variables

The concept of global variables plays a very vital role in C++ programming. It lets us use a variable anywhere within our whole program and change its values as per our requirements. The cause behind writing this guide is to introduce you to the concept of global variables in C++. After doing that, we also want to share with you some examples through which you will be able to understand the usage of global variables in C++ in Ubuntu 20.04 very clearly.

Global Variables in C++ in Ubuntu 20.04:

Depending upon the scope, the variables in C++ are broadly classified as local variables and global variables. The variable scope is distinct as the level to which that variable is accessible within your program. A local variable is the one whose scope is limited to a function, i.e., outside the body of that function, that local variable is inaccessible. A global variable is one whose scope spans the entire program, i.e., it can be retrieved anywhere within the program. A global variable is declared even before the “main()” function in C++, and it is destroyed only once your program finishes its execution.

Method of Using the Global Variables in C++ in Ubuntu 20.04:

For learning the usage of the global variables in C++, you will have to go through the three examples explained below. These examples have been implemented in C++ on a Ubuntu 20.04 system to teach you the basics of how the global variables work in C++.

Example # 1:

In this example, we intended to figure out the effect of adding two numbers on a global variable “sum”. For doing so, we have written the C++ program shown in the following image:

After including the required library and namespace in the C++ code shown in the image above, we have declared an integer type global variable named “sum”. We have not initialized this variable yet, which means that initially, this variable will hold a garbage value. After that, we have our “main()” function. Inside its body, we have declared two integer type variables named “a” and “b” and assigned them the values “10” and “20” respectively.

Now, we want to calculate the sum of these two variables by adding “a” and “b” and storing the result in our global variable “sum”. Therefore, we have the statement “sum=a+b” in our code. Then, we have simply printed the value of the “sum” variable on the terminal by using the “cout” statement. Finally, we have our “return 0” statement since the return type of our “main()” function was “int”.

Once we had written this C++ code to achieve the desired functionality, we simply saved this file as “GlobalVariables.cpp”. After saving it, we executed the command shown below in our terminal to compile our code:

$ g++ GlobalVariables.cpp –o GlobalVariables

After a successful compilation of our C++ code, we executed it using the following command:

$ ./GlobalVariables

When our C++ code was executed, the value of our global variable “sum” turned out to be “30” which is, in fact, the sum of our local variables “a” and “b” as shown in the image below. It means that the global variables can be accessed anywhere within our code, and their values can be easily manipulated as per our needs. Initially, we did not assign any value to our global variable “sum” but still, instead of displaying a garbage value on the terminal, the “sum” variable held the value “30” as its garbage value was overwritten as a result of the addition operation within our C++ code.

Example # 2:

In this example, we wanted to see how a function other than the “main()” function in C++ impacts the value of the global variable. For that, we have written the C++ program shown in the following image:

In the C++ code shown in the image above, we have declared a global variable named “variable” and initialized it with the value “10”. Then, we have defined the prototype of a function named “square()” which is intended to calculate the square of a number. After that, inside our main function, we printed our global variable. Then, we have incremented the value of this global variable, followed by printing this incremented value. Then, we have called our “square()” function from our “main()” function. Within this “square()” function, we have the statement “variable=variable*variable” that will simply calculate the square of our global variable hence updating the value of the “variable” yet again. Then, we have printed the value of this updated variable within the body of our “square()” function. Finally, we have the “return 0” statement within our “main()” function.

Then, after saving and compiling this code, when we executed it, we had three different values of our global variable printed on the terminal, as shown in the image below since we updated the value of this global variable twice within our C++ code. First, the initially assigned value of the global variable was printed, followed by its incremented value and then the squared value.

Example # 3:

In this example, we wanted to see the impact of changing the value of a global variable on a local variable declared with the very same name. The C++ code that we have written for this purpose is as follows:

In the C++ code shown above, we have declared a global variable of integer type named “variable” and assigned it the value “10”. After that, within our “main()” function, we have declared a local variable of integer type with the same name, i.e., “variable,” and assigned it the value “20”. Then, we wanted to print the values of both the local and global variables on the terminal. The value of the local variable can be printed as it is; however, for referencing the global variable with the same name, we will have to use the “::” operator before the name of our variable. After that, using the very same operator, we incremented the global variable. Then, we printed the values of both the local and global variables again, followed by the “return 0” statement.

After saving and compiling this C++ code, when we executed it, the initial values of our local and global variables were “20” and “10” respectively. However, when we incremented the global variable, it did not leave any impact on the local variable with the same name, i.e., only the value of the global variable got updated and became “11” whereas the value of the local variable stayed intact, i.e., “20” as you can see from the image shown below:

Conclusion:

This article was meant to develop a sound understanding of the global variables in C++ in Ubuntu 20.04. For that, we first began with the basic definition and purpose of a global variable depending upon its scope. After that, we walked you through three different examples through which you can see how global variables are used in C++. After reading through this whole guide, you will have sufficient knowledge of how global variables are declared and used in C++ in Ubuntu 20.04.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.