Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Nishtha Thakur

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.