How Does the Extern Keyword Work in C Language?

Click For Summary
The extern keyword in C is used to declare variables and functions that are defined in other source files, allowing for their use across multiple files. When extern precedes a variable declaration, it indicates that the variable is defined elsewhere, requiring one file to contain the actual definition without extern. This facilitates sharing of variables across different .c files. For functions, extern is implicit in declarations, meaning function declarations are treated as extern by default. The purpose of extern is to inform the compiler about symbol dependencies, aiding in the linking process where the linker resolves these dependencies into a single executable. Proper use of extern and header files ensures efficient code management and reduces the risk of errors when modifying shared variables or functions.
pairofstrings
Messages
411
Reaction score
7
Can you explain the working mechanism of extern keyword in C language? I have tried wiki and other sites but couldn't follow. Please explain in simple english.
 
Technology news on Phys.org
Hi pairofstrings! :smile:

I'll give it a try...

The extern keyword in front of an identifier tells the compiler that the actual definition of the identifier is supposed to be in another .c file.
After that you can use the identifier in your current .c file as if you had completely defined it.

Putting extern in front of a function declaration (without body) tells the compiler how the function should be called, so you can call the function in the current .c file.
The actual function definition with its body can then be defined in another .c file.

Extern can also be put in front of a variable declaration.
Perhaps this is a bit more confusing.
Doing that tells the compiler you want to share the variable between different .c files.
However, there has to be 1 .c file where the variable is declared without the extern in front of it.
 
I like Serena explained it, but I'd like to explain the background.

When source code is compiled, it is turned into "object code." Object code is a medium between the final executable and the original source code.

In object code, everything that can be accessed from another object code is given a name, referred to as a "symbol." For example, functions and global variables have symbols. The object code can also depend on symbols it doesn't contain. For example, it can call a function that isn't defined in the source code it was compiled from.

After all the objects have been compiled, they are sent to a linker that links the objects into a single executable. The linker makes sure that all the symbol dependencies are met. (In other words, it makes sure that, if one of the objects calls a function called "foo," then "foo" is defined in one of the objects or a library. Libraries are collections of symbols and their definitions.)

The extern keyword is to tell the compiler that the variable is defined somewhere else, and that the compiler should make that variable a dependency.
 
The relevant buzzwords in the C standard (and probably in your documentation) are "declaration" and "definition".

In a complete C program, each object (data item, function, etc) can only be DEFINED once. But if the source code is split into several different files, you have to compiler what the name means (in other words, DECLARE it) in every file that USES the name.

So, if your program uses a global array of integers called foo, there will be just ONE source file DEFINES foo, by sayng something like
Code:
int foo[100];

In the other files that use foo, you need to tell the compiler "foo is an integer array, but it's actually defined somewhere else". You do that by saying
Code:
extern int foo[];

Note, you don't need to tell the compiler how big the array is when you are only declaring it, because the compiler isn't going to allocate any storage for the array - you are just telling the compiler that foo IS an array.

Usually, each declaration appears just once in a header (.h) file, which is included everywhere it is needed. That way, if you suddenly realize that foo needs to be a long int, you only have to change two files (the .h file containing the declaration, and the file containing the definition), not a large number of .c files with the risk of forgetting one of them and introducing a bug into the program.

To make using header files easier, the compiler doesn't complain if it sees both a declaration and a definition of the same object, provided they don't contradict each other.

All the above also applies to functions, except that all function declarations are extern by default (because that is what you want them to be, 99.9% of the time!) So the function declarations
Code:
int foobar{double};
and
Code:
extern int foobar{double};
mean exactly the same thing. So in real life nobody uses "extern" on function declarations, only on variable declarations.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
86
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 59 ·
2
Replies
59
Views
8K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 133 ·
5
Replies
133
Views
10K
Replies
69
Views
10K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 122 ·
5
Replies
122
Views
16K
Replies
16
Views
4K