The extern keyword in C

    The extern keyword in C is used to tell the compiler that the variable that we are declaring was defined elsewhere. In order to fully understand this, you need to know the difference between a definition and a declaration of a variable.

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

Definition vs Declaration

  • Definition of a variable is when the variable is created and the memory for it is allocated.
  • Declaration of a variable just tells the compiler that this variable exists. It does not allocate any memory.

    By default it is up to the compiler to decide where to define and where to declare a variable.

  • The extern keyword forces a declaration of a variable:
    extern int externVariable;
  • You can force a definition of the variable by adding an initialization statement:
    int count = 5;

    When it comes to functions, we declare a function when we write its prototype.
We define a function when we write its body. Therefore there is no point of using the extern keyword with functions.

External(global) variables

    External variable is one that is defined out of any function. These variables are also called global. Extern keyword is of matter only to external variables in a project with more than one file.

Examples

Example 1:

file1.c

    int callCount = 0;

    int someFunction()
   
{
       
callCount++;
       
//do some stuff here
   
}

file2.c

    #include <stdio.h>

    int main()
   
{
       
extern int callCount;
       
printf("%d", callCount);
   
}

    In file2 we declare the variable callCount. Extern means that this variable is defined elsewhere and we will use that variable – we will not create a new variable here.

Example 2:

    In C, we use header files for all declarations. Usually we will put all extern variables there and we will not do any extern declarations in our source files. We will just include the header file and directly use the variables.

myHeader.h

    extern int callCount;
   
void printHello();

source1.c

    #include "myHeader.h"
    #include<stdio.h>

    int callCount = 0;

    void printHello()
    {
        printf("Hello, extern");
        callCount++;
    }

main.c

    #include "myHeader.h"

    int main()
   
{
   
    printHello();
        printHello();
        printHello();
        printf("The \"extern hello\"function was called %d times", callCount);
        return 0;
    }

    I prepared a zip file you. It contains the source and header files of the examples. You can download it, unzip and create projects with the source files: c-extern-examples.zip 

Summary

    In conclusion, the extern keyword in C forces a declaration of a variable, that was defined elsewhere. In practice it should be used in our header files and then we will include the necessary headers. We should avoid the usage of exern keyword in our source files.

Previous keywordenum

Next keywordfloat

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!


› extern

Did this help? Support me with your vote ;-)

Did this help?