In C and C++, the extern
keyword is used to declare a global variable or function in another file. Here's a comprehensive overview:
-
Global Variable Declaration:
extern
is used to declare a global variable or function in another file.- It's a way of informing the compiler that the variable or function is defined elsewhere.
-
Function Declaration:
- When used with functions,
extern
specifies that the function is defined in another file.
- When used with functions,
-
Variable Declaration:
extern int var;
- This line tells the compiler that the variable
var
is an integer and is defined in some other file.
-
Function Declaration:
extern int func(int, int);
- Declares a function
func
which is defined in another file.
-
In C:
- Functions are
extern
by default. - When you declare a function without any storage class, it is assumed to be
extern
.
- Functions are
-
In C++:
- Same as C, functions are
extern
by default.
- Same as C, functions are
-
Declaring Global Variables:
- Used to declare variables in header files which are defined in source files.
-
Sharing Functions Across Files:
- Functions defined in one file can be used in other files via
extern
declarations.
- Functions defined in one file can be used in other files via
-
External Linkage:
- Variables and functions with
extern
have external linkage, meaning they can be used in other files.
- Variables and functions with
-
Opposite Keyword -
static
:- While
extern
extends the visibility to other files,static
restricts the visibility within the file.
- While
-
Variable Example:
- File1.c:
int var = 10;
- File2.c:
extern int var;
(Accessvar
from File1.c)
- File1.c:
-
Function Example:
- File1.c:
void foo() { /* definition */ }
- File2.c:
extern void foo();
(Accessfoo
from File1.c)
- File1.c:
-
Header Files:
- Declaring
extern
variables and functions in header files is common to ensure that all source files which include the header are aware of these declarations.
- Declaring
-
Preventing Multiple Definitions:
- Helps in managing large code bases where functions and variables are spread across multiple files.
-
Definition Required:
extern
declares but does not define. So, there must be a definition in some other file.
-
Linker Errors:
- If the
extern
variable or function is not defined in any of the linked files, it will lead to linker errors.
- If the
-
Initialization:
extern
can't be used to initialize variables. It's only for declaration.
-
Avoid Global Variables:
- Use
extern
sparingly for variables. Prefer function parameters or other methods to share data.
- Use
-
Clear Module Interfaces:
- Use
extern
to clearly define module interfaces in header files, making the code easier to read and maintain.
- Use
-
Usage Similarity:
- The usage in C and C++ is mostly similar, with differences primarily in language features and not in the concept of
extern
.
- The usage in C and C++ is mostly similar, with differences primarily in language features and not in the concept of
-
Name Mangling in C++:
- In C++,
extern "C"
is used to prevent C++ name mangling when calling C functions.
- In C++,
- Purpose:
- Used to tell C++ compiler to use C-style naming and linkage for the specified code.
- Common Use:
- When including C headers in C++ programs or calling C functions from C++.