The Extern Function in C Language

Hey there coders! This post is all about concepts in C. Today we are going to discuss the Extern Function in C. It is one of the most important concepts in C programming. Hence, it is one of the most frequently asked interview questions. 

Additionally, you will also be able to use this function and keyword in your codes which are necessary. Thus, you will also benefit in improving your coding skills. And you can easily answer the questions related to extern functions in an interview.

So, without much delay, let’s get our coding brains to use and get started! 

Before we go into the Extern function, you need to know some more concepts prior to learning it. Since, we will use these concepts in explaining the Extern function in detail. 

Declaration and Definition

The extern keyword deals with the visibility of variables and functions in C. Therefore, it is important to be clear about the difference between declaration and definition.

Declaration:

We refer to declaration as a phenomenon which declares that the variable or function exist somewhere in the program. But, the system doesn’t allocate them any memory. 

Declaration of a variable or function  is important as it tells the program what type the variable or function belongs to. Additionally, in the case of function declarations, it tells the program the arguments, data types, the order of arguments and also the return type of the function.

For example, int x(int); this is a declaration.

Definition:

When we define a variable or a function, it does everything the declaration does but it also allocates memory. Therefore, we can say that definition is nothing but a superset of declaration. Definition is an important aspect of a program or code. Since, it serves as some sort of identity of a variable or a function. 

For example,

int x(int y)
{ return y; }

This is a definition. 

Note: We can declare a variable or function any number of times, but we can define it only once. 

What is the Extern keyword?

Extern keyword in c is used to extend the visibility of functions and variables in a program. We also use this keyword to define and declare external variables and functions. Hence, here are some properties of the extern keyword:

  • We can declare external variables a number of times but we can define them only once.
  • C Language makes the functions visible throughout the program by default. Hence, we do not need to declare or define “extern” functions which can increase the redundancy.
  • We can only declare variables with the “extern” keyword and we cannot define them.
  • And when we initialize an extern variable, we consider it as the definition of the extern variable. 

As you might already have noticed, we use the “extern” keyword with both function and variable. Therefore, let us see them in detail.

Using Extern keyword with functions

As said earlier, the C compiler treats the function declared as extern by default. Hence, when we write:  float abc(float a, float b); the compiler treats it as, extern float abc(float a, float b). 

Since the extern keyword extends the function’s visibility to the whole program, we can call the function anywhere in any of the files of the whole program.

But the files should contain the function declaration. Because, with the declaration of the function, the compiler will be sure that the definition of the function exists somewhere else. And it goes and compiles the file. 

Using Extern keyword with variables

Using the “extern” keyword with variables differs greatly from its use with function. For starters, the compiler doesn’t consider a variable declaration as extern. Therefore, we have to declare it explicitly. 

By using the “extern” keyword with variables, we make them visible to the entire program i.e the keyword extends the visibility of the variable to the whole program. Therefore, we can use the variable anywhere in the program. But we have to provide the declaration of the variable. 

Next, let us study some cases in which the extern will work with variables and cases it will not.

Example 1

For example, 

extern int x;
int main(void)
{
  return 0;
}

Here we have defined x as extern but we have not used x in the program. Therefore, compiler compiles this program successfully.

Example 2

Let us take an example where extern might not work.  

extern float x;
float main(void)
{
  x = 7.2;
  return 0;
}

Compiler will throw an error in this program. Because we have declared x but we have not defined it anywhere. Hence, the system has not allocated any memory to x. And the program is trying to change the value to 7.2 of a variable that doesn’t exist.

Example 3

Finally, an example where we can use the variable which we have declared as extern.

#include "file.h"
extern float x;
float main(void)
{
 x = 7.2;
 return 0;
}

In this example, we have assumed that file.h contains the definition of x. Hence, the compiler will find both the definition and declaration of x. Thus, it will compile the program successfully. 

Example 4

Let us consider a special case example,

extern int x = 0;
int main(void)
{
 x = 7;
 return 0;
}

If we declare a variable and also provide an initializer, then the system will allocate memory to the variable. Thus, the compiler will take the variable as defined. Therefore, following the C Language rules, the program will compile successfully.

What is extern function in C?

By now, you might already know what an extern function is. Even unknowingly we use the “extern” function frequently. Since by default any function that we define in a C file is extern. We can use these functions  in any other source file of the same project which has many other files.

Still, if you want to define it explicitly define them, this is how you do it:

  • extern int incr(int);
  • Or, extern int add(int a, int b) { return a+b; }

Benifits:

So, let us see some extensive benefits of an extern function in general.

If we use the “extern”, we are informing the compiler that whatever uses it, the compiler will find at link time. They don’t reserve anything for it in the current pass since, the compiler will encounter it later. Moreover, the compiler treats function and variable same in this regard.  

Using extern function is very useful if you need to share some global between modules and don’t want to put or initialize it in the header. 

Technically speaking, every function in a library public header is ‘extern’. However if we label them as extern it does little benefit depending on the compiler. Most compilers understand the implicit declaration but it is always safe to declare them. Additionally, we define those functions somewhere else.  

Externs are very useful for things like signal handlers, a mutex that you don’t want to put in a header or structure, etc. Usually, most compilers optimize to ensure that they don’t reserve any memory for external objects. Since they know it will reserve them in the module where you define the object. However, we specify an extern function with modern compilers when we work with prototyping public functions. 

Conclusion:

In this post, you have successfully learned about extern function in C. Using extern can be very useful as specified previously. Make sure to read and understand. And after that apply it in your codes to make them more efficient.

If you still have some doubts and query, you can ask us below. We would love to hear your views about the post.

See you in our next post!

Leave a Comment

Your email address will not be published. Required fields are marked *