How Does the Extern Keyword Work in C Language?

Click For Summary

Discussion Overview

The discussion centers around the functionality and implications of the `extern` keyword in the C programming language. Participants explore its role in variable and function declarations, particularly in the context of multiple source files and linking during compilation.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Exploratory

Main Points Raised

  • One participant requests a simple explanation of the `extern` keyword, indicating difficulty in understanding existing resources.
  • Another participant explains that `extern` indicates to the compiler that an identifier is defined in another source file, allowing its use in the current file.
  • It is noted that using `extern` with a function declaration informs the compiler about the function's calling convention, while the actual definition can reside in a different file.
  • A further explanation highlights that `extern` can also be applied to variable declarations, which allows sharing variables across different files, provided there is one file where the variable is defined without `extern`.
  • One participant elaborates on the compilation process, describing how object code is generated and how the linker resolves symbol dependencies, emphasizing the role of `extern` in indicating that a variable is defined elsewhere.
  • Another participant discusses the distinction between declaration and definition in C, explaining that each object can only be defined once, but must be declared in every file that uses it, often through header files.
  • It is mentioned that function declarations are `extern` by default, which is why they typically do not require the `extern` keyword explicitly.

Areas of Agreement / Disagreement

Participants generally agree on the basic functionality of the `extern` keyword and its implications for variable and function declarations across multiple files. However, there are nuances in the explanations provided, and no consensus on a single simplified definition is reached.

Contextual Notes

Some participants emphasize the importance of understanding the difference between declaration and definition, as well as the role of header files in managing declarations across multiple source files. There may be assumptions about familiarity with compilation processes that are not explicitly stated.

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.
 

Similar threads

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