Open In App

C++ Global Variables

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Scope of Variables, Data Types, and Functions in C++

In C++ programming languages, a variable is a name provided to memory to store different data types. Variable values can change anytime while running the program and each variable has its own scope (or region) where it is valid to access the variable using the name given to him.

In programming, variables are not all equal. Their scope, lifespan, and accessibility in the program depend on where and how they are declared. There are two types of variables based on their scope.

  1. Local variable – The scope of these variables exists only within the block in which the variable is declared. i.e. we can access this variable only within that block.
  2. Global variable – Global variables are a special type with the widest scope possible. It is declared outside of all of the functions and blocks, at the top of the program. They can be accessed from any portion of the program.

Global Variable in C++

Global variables are the variables that are declared outside of any function or class and can be accessed by any part of the program. They are generally declared at the beginning of the source file after the header file. They are available throughout the lifetime of a program and accessible from anywhere within the program.

Declaration of a Global Variable in C++

To create a global variable, we simply declare it at the top of the source file, after the header files, and before the main function. In C++, all the variables must be declared before use.

Example 1

C++




// C++ Program to illustrate Global Variable
// header files
#include <iostream>
using namespace std;
  
// global variable
int x = 10; // x is a global variable initialized to 10
  
// main function
int main()
{
    cout << x;
    return 0;
}


Output

10

Explanation

  • In this example, `x` is a global variable that can store an integer value.
  • It is initialized to 10 when the program starts.
  • Since it is declared outside any function or class, it can be accessed and modified by any function or class in the program.

Now, someone might wonder why you would want to use global variables in your program.

Example 2

C++




// C++ program to illustrate
// usage of global variables
#include <iostream>
using namespace std;
  
// global variable
int global = 5;
  
// global variable accessed from
// within a function
void display() { cout << global << endl; }
  
// main function
int main()
{
    display();
  
    // changing value of global
    // variable from main function
    global = 10;
    display();
}


Output

5
10

Explanation

  • In this example ‘int global’ is a Global Variable that stores an integer value.
  • It initialized with 5 when the program started.
  • After that call goes to the main function and then it calls the display function which prints the global variable.
  • After printing 5, the value of the global variable is changed to 10. Now again the display function is called and the new value of the variable global 10 is printed.

Benefits of Using Global Variables

Following are some main benefits that global variables provide:

  • Global Variable can be accessed directly by all the functions without passing an argument in the program.
  • Global Variables are very useful when many functions access the same variable.
  • Global Variable required only a one-time declaration in the program.

Drawbacks of Using Global Variables

Global variables also come with some drawbacks. Some of them are:

  • Sometimes Global Variable can cause conflict issues as multiple programs try to modify them at the same time, generally in multithreading programs.
  • It can sometimes lead to variable shadowing.
  • Global Variables can make code less readable and less maintainable.
  • Also, there is some security and bugs concern as they can be modified or accessed by any part of the program.
  • Sometimes if you use a larger number in the global variable there is a high chance of error in the program.

Conclusion

Global variables are very useful but ‘difficult to track’ variables in C++ programming language. They are useful because we can access the same variable in any part of the program but it must be used such that it doesn’t make code less readable and less maintainable, and also takes care of security aspects. Therefore, it is suggested to use global variables rarely and carefully and prefer local variables or other alternatives whenever possible.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads