C Programming External Variables

Last modified by Microchip on 2023/11/09 09:06

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​

1
extern int x;

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

1
2
3
4
5
6
7
8
int foo(int x)
{
   extern int a;
    ...
   return a;
}

int a;

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

1
2
3
4
5
6
7
extern int x;

int main(void)
{
    x = 5;
    ...
}

SomeFileInProject.c

1
2
3
4
5
6
int x;

int foo(void)
{
    ...
}

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.