
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to declare a global variable in C++
To declare global variables in C++, we can declare variables after starting the program. Not inside any function or block. If we want to declare some variables that will be stored in some different file, then we can create one file, and store some variable. For some external file sometimes we need to put the extern keyword with it. Also we have to include the external file with the actual program file.
Example
extern int x = 10; extern int y = 20;
Example
#include<iostream> #include"global.cpp" using namespace std; int main() { cout << x << endl; cout << y << endl; }
Output
10 20
- Related Articles
- How to declare a global variable in PHP?
- How to declare a global variable in Python?
- How do I declare a global variable in Python class?
- How to declare global variables in Android?
- How to declare global Variables in JavaScript?
- How to declare a variable in MySQL?
- How to declare a variable in C++?
- How to declare a variable in Python?
- How to declare a local variable in Java?
- How to set a global variable in Postman?
- How to create a Global Variable in Postman?
- MySQL how to declare a datetime variable?
- How to define global variable in a JavaScript function?
- How to declare a variable correctly in a MySQLProcedure?\n
- How to declare a variable inside a procedure in MySQL?

Advertisements