- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are global variables in C++?
Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the lifetime of your program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.
Example
#include <iostream> using namespace std; // Global variable declaration: int g; int main () { // Local variable declaration: int a, b; a = 10; b = 20; g = a + b; cout << g; return 0; }
Output
This will give the output −
30
- Related Articles
- What are local variables and global variables in C++?
- Why are global variables bad in C/C++?
- Global and Local Variables in C#
- What are the rules for local and global variables in Python?
- How are C++ Local and Global variables initialized by default?
- Global variables in Java
- Initialization of global and static variables in C
- Why are global and static variables initialized to their default values in C/C++?
- Global Scope Variables in Postman?
- Global Variables in Lua Programming
- Why should we avoid using global variables in C/C++?
- Why we do not have global variables in C#?
- Global and Local Variables in Python?
- Global and Local Variables in Java
- Global vs Local variables in Python

Advertisements