Variables that are defined outside the scope where they are used
Sometimes, we will want to access a variable that is defined someplace we normally couldn't access, such as in a separate file, or someplace after the location in a program where we want to use the variable.
Still need to be declared within the scope where they are used
Even though the variable is defined someplace within our project, it is still required to be declared before it may be used. When you declare a variable, no memory is allocated. You are simply telling the compiler about its characteristics (name and type). The code will ultimately be connected where the variable is allocated in memory during the link process.
extern keyword used to tell compiler
The extern keyword is used to declare a variable that is defined elsewhere. As with function prototypes, an extern variable declaration doesn't allocate memory. It merely identifies the signature of the variable so that the compiler knows how it should be handled when it is encountered in your code.
extern type identifier;
External Variable Declaration Example
A variable declared as an extern within a function
This is not the most common use of extern, but it does work and can be useful in some circumstances. When a local variable is declared as extern, that means that no space is allocated for the variable from within the function. It simply tells the compiler/linker that the variable is defined elsewhere – the memory will be allocated somewhere else. This is much like when we declare a function prototype so that the function may be called before it is defined.
The definition of the variable may be after the function in which it is used, or it may be in another file altogether. One possible way this might be useful is to create a global variable in a separate source file. Then, if you declare it inside multiple functions as an extern, it will only be accessible from within those functions unless it is declared as an extern at the top of the source file where the functions are called. This can provide a rudimentary form of something known as data hiding, which is a fundamental rule of good programming practice in object-oriented languages such as C++.
Example
A variable declared as extern outside of any function
If we declare a variable as extern outside of any function, this means that the variable is defined in another file altogether (Note: This may also be true for a variable declared as extern within a function, but in that case, the variable may just be defined later on within the same file.) No memory is allocated where the variable is declared as extern. This is simply a way to tell the compiler that the variable named will be used in this file, but it will be defined, and memory will be allocated for it in another file.
Main.c
SomeFileInProject.c
In the example shown, the variable x is defined in the file SomeFileInProject.c. It is a global variable, and therefore is visible from anywhere in the project (not just within its own file). However, to use it in Main.c, it is necessary to declare it as extern so that the compiler knows that we are referring to the variable x that is declared in SomeFileInProject.c. Note that this is a good example of why unique variable names are a good programming practice (unlike the simple single letter names we've been using). When we declare x as extern in Main.c, the variable x must only be defined globally in one other file in the project, otherwise, the compiler won't know which variable x we are referring to.
Once again, this is analogous to having a function prototype. If we want to call a function in another file, we need to declare its prototype in the file that will be calling the function. This will be discussed in more detail in the section on multi-file projects.