Table of Contents
Extern keyword In C
The “extern” keyword in the C programming language is a fundamental and versatile element that extends the reach and accessibility of variables and functions. Named for its role in “external” visibility, “extern” plays a pivotal role in enabling inter-file communication and code modularity. By employing “extern,” programmers can declare variables and functions that are defined in other source files, fostering the creation of efficient and organized software systems. In this exploration of the “extern” keyword in C, we will look into its significance, usage, and its role in enhancing the flexibility and scalability of C programs.
- Declaration: A declaration simply informs the program that a variable or function exists somewhere within the program, providing information about its type and, in the case of functions, its arguments and return type. Declarations do not allocate memory.
- Definition: A definition encompasses everything that a declaration does but goes a step further by allocating memory for variables or providing the implementation for functions. A definition is a superset of a declaration.
The term “extern” is short for “external” and is used when one file needs to access a variable defined in another file. “Extern” enables the sharing of variables between different parts of a program, promoting modularity and code organization. This keyword is crucial for enabling inter-file communication in C programs.
Syntax of extern in C
The syntax to define the extern variable in C is as follows:
extern data_type variable_name;
Example for Extern Variable in C
#include <stdio.h>
extern int a; // int var; -> declaration and definition
// extern int var; -> declaration
int main()
{
printf("%d", a);
return 0;
}
Properties of extern Variable in C
- When you declare an extern variable using a statement like
extern some_data_type some_variable_name;
, you are not allocating memory for the variable; instead, you are announcing the variable’s properties and type. This tells the compiler that the variable is defined somewhere else in the program. - Multiple declarations of an extern variable within the same file are allowed. This is different from automatic (local) variables, which can only be declared once within the same scope.
- The role of the extern variable is to inform the compiler that the variable’s definition exists outside of the current scope. The compiler relies on this information and produces no error at compile time.
- However, it’s the linker’s responsibility to ensure that the variable is defined elsewhere in the program. If the linker does not find a matching definition for the extern variable, it will generate an error.
- When an extern variable is initialized, memory is allocated for it, and it is considered defined. Initialization typically occurs when a variable is given an initial value in the source code.
This understanding of extern variables is essential for enabling communication and sharing of variables across different source files in a C program.
Note: A variable can be declared many times, but defined only once.’
Use of Extern in functions
We know that when a function is declared or defined. The extern keyword is assumed.
int foo(int arg1, char arg2);
The compiler treat it as the follows:
extern int foo(int arg1, char arg2);
The “extern” keyword, when used with functions in C, makes the function accessible throughout the entire program. Other program files need only contain a declaration of the function to use it. The compiler understands that the function’s definition exists elsewhere. This promotes modularity and organized code across multiple files.
Use of Extern with variables
extern int var;
We can understand that an integer-type variable called var is declared here.
To Define Var
int var = 10;
- When you declare and define an integer variable, such as
int var;
, it’s considered a definition. In this case, memory is allocated for the variable. - Unlike functions, where the “extern” keyword is implicitly assumed, variables do not have this implicit behavior. If you want to declare a variable without defining it (i.e., without allocating memory), you need to include the “extern” keyword explicitly, like
extern int var;
. - Using the “extern” keyword with a variable extends its visibility to the entire program, allowing you to use the variable anywhere in the program, provided you include its declaration. The declaration informs the compiler that the variable’s definition is located elsewhere in the program.
This distinction helps control memory allocation for variables and ensures that you can share variables across different parts of the program when necessary while maintaining clarity and organization in the code.
Example 1
int var;
int main(void)
{
var = 10;
return 0;
}
In the above program, we can conclude that the program compiles successfully and var is defined.
Example 2
extern int var;
int main(void)
{
return 0;
}
This program is successfully compiled and the var is defined. We can analyze further that var is never used and therefore, no problems have come.
Example 3
extern int var;
int main(void)
{
var = 10;
return 0;
}
The explanation regarding the error in the program is accurate. If you declare a variable using the “extern” keyword but do not provide a corresponding definition for that variable, the variable is considered declared but not defined. This means that memory is not allocated for the variable, and the program is essentially trying to change the value of a variable that does not exist in memory.
Example 4
// As we are importing the file and henceforth the
// defination
#include "somefile.h"
// Declaring the same variable
extern int var;
// int var;
// It will throw compiler error as compiler will get
// confused where the variable is defined
int main(void)
{
var = 10;
return 0;
}
// Now it will compile and run successfully
Output
10
- When a variable is declared in one file and defined (given memory allocation) in another file, you typically use the “extern” keyword in the file where the variable is declared. This informs the compiler that the variable’s definition exists elsewhere.
- However, it’s important to note that if you also include the same file in which the variable is defined (i.e., the file that contains the definition), using the “extern” keyword in that file can lead to a compiler error.
- This is because, in the file where the variable is defined, there is no need for the “extern” keyword. If you include it there, it may create a new memory block for the variable, rendering the use of “extern” unnecessary.
Example 5
extern int var = 0;
int main(void)
{
var = 10;
return 0;
}
When a variable is declared with an initializer, memory is allocated for that variable, and it is considered defined. In this case, the program will compile successfully, and the variable will work as expected.
Initialization provides an initial value to the variable, and the memory for the variable is allocated at that point. As a result, there is no need to use the “extern” keyword because the variable is both declared and defined in the same file. This allows the variable to be used in the program without issues.
FAQ- Extern keyword In C
Q1. What is the use of extern C?
Ans. In C++, “extern ‘C'” specifies that a function is defined elsewhere and follows the C-language calling convention, often used for interfacing with C libraries. It can be applied to multiple function declarations within a block. For templates, “extern” indicates that the template has been instantiated elsewhere, ensuring proper linkage in the program.
Q2. What is the difference between global and extern in C?
Ans. Global variables are accessible throughout the entire program and have allocated memory. Extern variables are also program-wide but only declare the variable without memory allocation, relying on an external definition.
Q3. What is the difference between include and extern?
Ans. When you use “#include” in your code, it’s like copying and pasting all the functions and variables from another file into your current file. And when you see “extern” in front of a variable or function, it means that it’s defined in some other file, but you can still use it in your current file.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together