How Does the Extern Keyword Work in C Language?

In summary, 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. 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
  • #1
pairofstrings
411
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5


The extern keyword in C language is used to declare a variable or function that is defined in another source file. This allows the program to access the variable or function from the external source file without having to redefine it in the current file.

The working mechanism of the extern keyword involves two steps. First, the variable or function is declared using the extern keyword in the current file. This tells the compiler that the variable or function is defined in another source file.

Next, in the external source file, the variable or function is defined without using the extern keyword. This tells the compiler that the variable or function is being defined and allocated storage space.

During the compilation process, the linker will combine the declaration and definition of the variable or function, allowing the program to access it from the external source file.

In simple terms, the extern keyword allows us to use variables or functions from other files in our program without having to redefine them. It helps to keep our code organized and avoid duplicate definitions.
 

What is the "extern" keyword in C language?

The "extern" keyword in C language is used to declare a variable or function that is defined in another source file. It is typically used in conjunction with the "extern" storage class specifier to indicate that the variable or function is defined externally.

When should the "extern" keyword be used?

The "extern" keyword should be used when a variable or function needs to be accessed from multiple source files in a C program. By using "extern", the variable or function can be declared in one source file and then referenced in other source files without having to redefine it.

Can the "extern" keyword be used with local variables?

No, the "extern" keyword cannot be used with local variables. It is meant to be used with global variables or functions that are defined in other source files. Local variables are only accessible within the scope of the function they are declared in.

Is the "extern" keyword necessary when including header files?

Not necessarily. Including header files is one way to make variables and functions accessible in multiple source files without using the "extern" keyword. However, if the header file only contains declarations and not definitions, then the "extern" keyword would be needed when defining the variables or functions.

Can the "extern" keyword be used in C++?

Yes, the "extern" keyword can be used in both C and C++ languages. However, it is more commonly used in C since C++ has other mechanisms such as namespaces to achieve similar functionality.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
878
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
7K
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
Replies
6
Views
1K
Back
Top