How to Declare a Global Variable in C++? Last Updated : 23 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, global variables are like normal variables but are declared outside of all functions and are accessible by all parts of the function. In this article, we will learn how to declare a global variable in C++. Global Variable in C++ We can declare a global variable, by defining it outside of all functions at the top after the header files. Once the global variable is declared, it can be accessed from the scope of the main function as well as any inner construct (loop or if statement) inside it. Syntax to Declare Global Variable in C++A global variable is declared in the same way as any other variable but it is placed outside of every function. dataType globalVariableName = variableValue;C++ Program to Declare Global Variables The below example demonstrates how we can declare global variables in C++. C++ // C++ program to declare a global variable #include <iostream> using namespace std; int g_value = 10; // declaring the global variable void display() { cout << "Global variable value: " << g_value << endl; } int main() { display(); // can access the global variable from a // function cout << "Global variable value: " << g_value << endl; // can access the global variable from main return 0; } OutputGlobal variable value: 10 Global variable value: 10 Note: Avoid the excessive use of global variables because it makes a program hard to understand and debug. Comment More infoAdvertise with us Next Article How to Declare a Global Variable in C++? H heysaiyad Follow Improve Article Tags : C++ Programs C++ CPP Examples Practice Tags : CPP Similar Reads How to Declare a Vector in C++? In C++, the vector is a dynamic array that can resize itself automatically to accommodate new elements. In this article, we will learn how to declare a vector in C++. Declare a Vector in C++In C++, vectors are defined as the class templates in <vector> header file. So, to declare a std::vector 2 min read How to Declare a Static Variable in a Class in C++? In C++, a static variable is initialized only once and exists independently of any class objects so they can be accessed without creating an instance of the class. In this article, we will learn how to declare a static variable in a class in C++. Static Variable in a Class in C++To declare a static 2 min read How to Get Environment Variable in C++? Environment variables are globally accessible named values that store information about the system environment where your code is executed. They are also used to store configuration settings, paths to important directories, and other system-specific data as well. In this article, we will learn how t 2 min read How to Access Private Variable in C++? In C++, Private members of a class are accessible only within the class they are declared and by friend functions, they are not accessible outside the class not even by derived classes. In this article, we will learn how to access private variables in C++. Accessing Private Variables From Class in C 2 min read How to Declare an Array in C++? In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of 2 min read Like