What is Extern Keyword in C?

What is Extern Keyword in C?

5 mins read1K Views Comment
Updated on Oct 3, 2023 11:53 IST

This blog will make you understand the Extern keyword in C programming. We have explained this concept with the help of examples. Let’s understand!

2023_03_Extern-Keyword-in-C.jpg

The “extern” keyword is an important tool for managing variables and functions in large C programs. It allows for greater modularity and code reusability. In this article, we will discuss the “extern” keyword in C and understand its use with the help of examples. We will be covering the following sections: 

Explore: Online C Programming Courses

Introduction to Extern Keyword in C 

In C, the extern keyword is utilized to signify a variable as a global variable. This means that it can be accessed from anywhere in the program, even if it was declared in a different scope of the same file or another file. Variables or functions declared with “extern” in C will have external linkage, indicating that they are linked externally. 

Declaring Variables in C 

Declaring a variable denotes that the variable’s data type is introduced to the compiler. Defining a variable, on the other hand, entails the compiler allocating the necessary storage for the variable. While a variable definition can also be considered a declaration, this is not always the case. 

It should be emphasized that in C, the “extern” keyword is solely employed for declaring variables, not for defining them. 

In a lengthy program that utilizes several files, the “extern” keyword is frequently employed to declare global variables in header files. It will be used in other files. 

Note that once the variable is declared, its data type cannot be altered. 

Syntax of extern 

The syntax of the “extern” keyword is in C is given as: 


 
extern data_type variable_name;
extern return_type function_name(arguments);
Copy code

Here, the “extern” keyword is used before the declaration of a variable or function to signify that it is defined in a different file or scope. The variable or function is then referenced in the current file by using the same name and data type, but without re-declaring it. 

Note that the “extern” keyword is not necessary if the variable or function is defined in the same file as it is referenced. In that case, it is sufficient to simply declare it without the “extern” keyword. 

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4.24 K
6 weeks
475
6 weeks
– / –
45 hours
4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
4 K
80 hours

Examples of Using the Extern Keyword in C  

Here are some examples of using the “extern” keyword in C: 

Example 1: Using extern with global variable 

In this example, we have a global variable “x” defined in one file and we want to use it in another file. We declare the variable as “extern” in the second file to let the compiler know that it is defined externally. 

File 1: 


 
int x = 10;
Copy code

File 2: 


 
extern int x;
int main() {
printf("The value of x is %d", x);
return 0;
}
Copy code

Output: 

The value of x is 10 

Example 2: Using extern with function 

In this example, we have a function “multiply” defined in one file and we want to use it in another file. We declare the function as “extern” in the second file to let the compiler know that it is defined externally. 

File 1: 


 
int multiply(int a, int b) {
return a * b;
}
Copy code

File 2: 


 
extern int multiply(int a, int b);
int main() {
int result = multiply(5, 6);
printf("The result is %d", result);
return 0;
}
Copy code

Output: 

The result is 30 

In both of these examples, the “extern” keyword is used to declare a variable or function as defined externally, allowing it to be used in a different file. 

What is C Extern Function?  

In C programming language, an “extern function” is a function. It is declared in one file but defined in another file. It is similar to an “extern variable,” which is a variable that programmers declare in one file and defined in another file. 

Extern function allow a function to be used in multiple files without having to redefine it in every file. By declaring the function as “extern,” the compiler knows that the function is defined in another file. Also, it can link to it during the linking phase of compilation. 

Syntax of extern function 

The syntax of “extern” function is in C is given as: 


 
extern return_type function_name(arguments);
Copy code

Here, the “extern” keyword is used before the declaration of the function. This is done to indicate that it is defined in another file or scope. The function is then referenced in the current file by using the same name, return type, and arguments, but without redefining it. 

Variables in C Programming: Types, Scope, and Lifetime
Variables in C Programming: Types, Scope, and Lifetime
Variables in C Programming are a container to hold the value of a data type. They help the compiler to create memory space for the type of Variable. Each variable...read more
Structure in C Programming: How to Create and Use Programs
Structure in C Programming: How to Create and Use Programs
This article is a complete guide to understanding the meaning of C structure and by the end of this article you will understand how to use and when to use...read more
Malloc in C Programming: How to Allocate Memory Dynamically 
Malloc in C Programming: How to Allocate Memory Dynamically 
This article covers the concept of dynamic memory allocation in C programming, which allows allocating memory at runtime according to the program’s needs using functions like malloc(), calloc(), realloc(), and...read more

Examples of Using the Extern Function in C  

Here are some examples of how to declare and define an “extern function” in C: 

Example 1: Using extern with a function defined in another file 

In this example, we have a function “add” defined in one file and we want to use it in another file. We declare the function as “extern” in the second file to let the compiler know that it is defined externally. 

File 1: 


 
// Define the function
int add(int a, int b) {
return a + b;
}
Copy code

File 2: 


 
// Declare the function as extern
extern int add(int a, int b);
int main() {
int result = add(5, 6);
printf("The result is %d", result);
return 0;
}
Copy code

Output: 

The result is 11 

Example 2: Using extern with a function defined in a header file 

In this example, we have a function “multiply” declared in a header file that we want to use in our main program. We include the header file and declare the function as “extern” to let the compiler know that it is defined externally. 


 
// Declare the function
extern int multiply(int a, int b);
Copy code

Main Program:


 
// Include the header file
#include "multiply.h"
int main() {
int result = multiply(5, 6);
printf("The result is %d", result);
return 0;
}
Copy code

File defining the function (multiply.c): 


 
// Define the function
int multiply(int a, int b) {
return a * b;
}
Copy code

Output: 

The result is 30 

In both of these examples, the “extern” keyword is used to declare a function as defined externally, allowing it to be used in a different file or header file. 

Endnotes 

Hope this article was helpful for you to understand the extern keyword in C. Also, if you want to learn more about C programming and solidify your basics, you can explore our articles on C. 

Contributed By: Prerna Singh

FAQs

What is the difference between declaring a variable as

Declaring a variable as "extern" tells the compiler that the variable is defined elsewhere and can be used in the current scope. Defining a variable allocates memory for it and assigns an initial value.

Can you use the

Yes, you can use the "extern" keyword with a function definition in the same file, but it is not necessary. The "extern" keyword is used to indicate that a function is defined in another file or scope.

What is the scope of an

An "extern" variable has global scope, which means it can be accessed from any function in the program.

Can you initialize an

No, you cannot initialize an "extern" variable at the point of declaration because it is only a declaration and not a definition. The variable is defined in another file or scope.

How do you link an

The linking of an "extern" function is done automatically by the linker during the linking phase of compilation. The function is declared in one file and defined in another file or scope, and the compiler links the call to the function to the definition of the function.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio