
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The extern Storage Class in C++
The extern storage class specifier lets you declare objects that several source files can use.
What is Extern Storage Class in C++?
The extern storage class in C++ is used to declare an object (global variable or function) that can be accessed by multiple source files. When a variable is declared with extern, it tells the compiler that the variable or function exists in another file (or later in the same file) and that memory is not allocated at the point of declaration.
Syntax
Here is the following syntax for the extern storage class in C++. Here, it's a declaration, but no memory allocated:
extern data_type variable_name;
Example of Extern Storage Class
You can use the extern keyword as follows to share the variable across files. Here using the two following files to show the working of it in real life scenarios.
File 1: file1.cpp
In this file, we will define the global variable.
#include <iostream> using namespace std; int count = 42; // Defining a global variable void showCount() { // Defining a global function cout << "Count is: " << count << endl; }
File 2: file2.cpp
In this file, we will declare the global variable using extern and will access the value of the global variable count = 42 and global function showount(), which are defined in file1.cpp, to use them in file2.cpp.
#include <iostream> using namespace std; extern int count; // Declaration of the global variable void showCount(); // Declaration of the function int main() { cout << "Accessing count from file2.cpp (which is defined in file1.cpp): " << count << endl; showCount(); // Function from file1.cpp return 0; }
To compile and run the above given files of code using g++.
g++ file2.cpp file1.cpp -o program ./program
Output
Accessing count from file2.cpp (which is defined in file1.cpp): 42 Count is: 42
Where Extern Storage Class can be Used?
You can write and use extern in two main contexts.
1. Outside functions (Global Scope)
The extern keyword is used outside functions (global scope), which helps to declare a variable or function that is defined in another file or elsewhere in the program.
// file1.cpp int count = 42; // definition of a global variable // file2.cpp extern int count; // declaration of the global variable defined in file1.cpp
2. Inside a function (Block Scope)
It can also be used inside the function (at the beginning of a block), which helps it to access a global variable inside a function's local scope, when defined in another file.
void myFunction() { extern int count; // accessing the global variable 'count' cout << count; }
Extern is Optional
The use of extern storage class is optional for functions and external linkage variables. Let's understand with the help of examples.
1. For function
In C++, the function declarations always have external linkage by default. Therefore, you don't need to explicitly use extern while declaring functions, whether it is in the same file or across different files.
// function declaration with 'extern' (optional) extern void printData(); // function declaration without 'extern' (this is also valid) void printData();
2. For global variables
The extern is optional when accessing global variables within the same file (as it has external linkage by default), but it's not optional when accessing global variables across different files.
// global variable definition int count = 10; // access without 'extern' (valid) cout << count; // access with 'extern' (still valid as it's optional in the same file) extern int count; cout << count;
So, Functions and global variables have external linkage by default, so in these cases, you don't need to explicitly define extern for these.
Limitation
- The extern can only be used with variables or functions, but not with data types (like int, char, bool, etc).
- The extern cannot be used inside a class definition.