C extern keyword
In the C language, usingexternVariables declared with the extern keyword are external variables, indicating that the variable is defined elsewhere (usually in another file).
externThe extern keyword is used in C to declare external variables or functions, allowing them to be shared and used across multiple files.
externIt is mainly used to implement modular programming and code separation.
externThe declaration and definition of variables are usually placed in different files.
extern variable
The following demonstrates how to define an external variable in one file and declare and use it in another file.
1. Define and initialize (in one file, file1.c)
Example
// Define and initialize external variables
int d = 3;
int f = 5;
void printExternVariables() {
printf("d: %d, f: %d\n", d, f);
}
2. Declare and use (in another file, file2.c)
Example
// Declare external variable
extern int d;
extern int f;
// Declare the external function (must be declared before calling)
extern void printExternVariables();
int main() {
// Use external variable
printf("d in main: %d\n", d);
printf("f in main: %d\n", f);
// Call a function defined in another file
printExternVariables();
return 0;
}
Compile and run file1.c and file2.c together (see compilation command below), the output is:
d in main: 3 f in main: 5 d: 3, f: 5
extern function
Similar to variables, functions can also be defined in one file and declared and used in another file.
1. Define a function (in one file, file1.c)
Example
void printMessage() {
printf("Hello from printMessage!\n");
}
2. Declare and use the function (in another file, file2.c)
Example
// Declare external function
extern void printMessage();
int main() {
// Call external function
printMessage();
return 0;
}
3. Compiling multiple files
To use extern variables and functions, all related files must be compiled together. For example:
gcc file1.c file2.c -o program ./program
After compiling and running the function example above, the output is:
Hello from printMessage!
Notes
UsageexternThe following points should be noted:
| Points to note | Description |
|---|---|
| Definition and initialization | External variables must be defined and initialized in one file, and only declared in other files.externDeclarations cannot be initialized. |
| Scope | externVariables and functions have global scope and can be accessed anywhere in the program. |
| Redefinition | Avoid defining the same variables or functions repeatedly in multiple files, which would cause linker errors. |
| Header file usage | UsuallyexternPut the declarations in a header file, and then include that header file in the source files that need it. |
Usage of header files
To simplify management, you can use a header file to declare extern variables and functions.
variables.h file code:
#define VARIABLES_H
// Declare external variable
extern int d;
extern int f;
#endif
functions.h file code:
#define FUNCTIONS_H
// Declare external function
void printExternVariables();
void printMessage();
#endif
Then include the header file in the source file:
file1.c file code:
#include "variables.h"
#include "functions.h"
// Define and initialize external variables
int d = 3;
int f = 5;
void printExternVariables() {
printf("d: %d, f: %d\n", d, f);
}
void printMessage() {
printf("Hello from printMessage!\n");
}
file2.c file code:
#include "variables.h"
#include "functions.h"
int main() {
// Use external variable
printf("d in main: %d\n", d);
printf("f in main: %d\n", f);
// Call external function
printExternVariables();
printMessage();
return 0;
}
Also usegcc file1.c file2.c -o programCompile and run, the output result is:
d in main: 3 f in main: 5 d: 3, f: 5 Hello from printMessage!
In this way, managing extern declarations through header files makes the code clearer and easier to maintain.
other extensions