In C extern is a keyword that 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.
By default it is up to the compiler to decide where to define and where to declare a variable.
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 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.
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.
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.
Here's how this examples should work:
You should also try it and move things around to get the idea.
The sources from the examples above are availble on GitHub. I also 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
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.