What is the purpose of the extern keyword in the C language?
In the C language, the extern keyword is used to declare the external linkage of variables or functions. Its main purpose has two uses:
- external
For example, if a global variable int globalVar is defined in file A.c, and this variable needs to be used in file B.c, the syntax “extern int globalVar;” can be used in file B.c to declare the variable, allowing the compiler to know that it has already been defined elsewhere.
- external
For example, if we define the function int func(); in file A.c, and we need to call this function in file B.c, we can declare the function in file B.c using extern int func(); so that the compiler knows the function has been defined elsewhere.
It’s important to note that the ‘extern’ keyword is used only for declaring variables or functions, not for defining them. The actual definition of variables or functions typically happens elsewhere.