Welcome to the seventh part of the Gentle Introduction to C series. As said before this series aims to provide you a brief introduction to C language.
In this part, we will be talking about the keyword extern.



However before talking about about extern keyword, let us just refresh our mind with the understanding of declaration and definition.


Declaration : Declaration of a variable or a function, as the name suggests, declares the variable or the function. However, during the declaration of a variable or function no memory is allocated for the entities. The sole purpose of declaration is to tell the compiler or the program about the type of variable or number of arguments, type of each argument,return type (in case of functions).


Definition : Definition of a variable or function, as the name suggests, defines the variable or the function. Definition of a variable or function, in addition to what declaration does, allocates memory for the entities. Hence, you can consider definition as a super set of declaration i.e. all definitions are declarations but all declarations are not definitions.



Note : Declaration of a variable or function can occur many times. However, a variable or function can only be defined once. This is due to the fact that a single variable  or function cannot be defined at multiple memory locations.
Now let's talk about extern keyword.


extern Keyword

The sole purpose of extern keyword is to extend the visibility of a variable or function. By visibility here I mean the scope of the variable or the function.


In case of variables, extern keyword can also be used to declare a variable.
Let us try to understand this using an example.

int a;

In the line above the variable a is defined i.e. memory has been allocated to the variable a.

extern int a;


In the line above the variable a is declared only i.e. memory has not been allocated to the variable 'a'.
So, if write a code like,        

extern int a; 

int main() {            
    a=0;            
    return 0;        
}

The code above will throw an error, as the variable a is only declared and not defined. So the code above essentially is trying to change the value of a memory location which doesn't exists.
So, we can modify the code as,        

#include "fileContainingDefinitionOfa.h" 

extern int a;        

int main() {            
    a=0;            
    return 0;        
}

Assuming that the file fileContainingDefinitionOfa.h contains the definition of variable a, the above code will compile successfully.


Note : If the variable declared using extern keyword is initialised, then the compiler allocates memory to the variable and hence implicitly defines the variable.
Example :        

extern int a=0;        

int main() {            
    a=100;            
    return 0;        
}

The above code will not throw any errors.



In case of functions, extern keyword is used to extend the visibility of the function. Since, the extern extends the visibility of the function to the whole program, the function can be called from any files provided that those files contains the declaration of the function.With functions, the extern keyword is implicitly assumed whenever a function is declared or defined i.e.        

int foo (int a, int b)

is treated by the compiler as      

extern int foo (int a,int b)


At last, using extern keyword is a great way to define and declare global variables and often useful when certain functionalities and variables are to be shared among several files.


Thanks for reading.