These keywords used to literally haunt my dreams back in the day, when I was a
freshman at BUT. I was just getting grips with C at
that time and whenever I tried to use them, I got it all wrong. I’m talking
about the magic keywords –
extern . Both of them have multiple
uses in C code and slightly different behavior in each case. For a beginner it
might seem like a total anarchy, but it will start to make sense at some point.
So, let’s get to the point…
Both of these modifiers have something to do with memory allocation and linking of your code. The C standard refers to them as storage-class specifiers. Using those allows you to specify when to allocate memory for your object and/or how to link it with the rest of the code. Let’s have look on what exactly is there to specify first.
Linking in C
There are three types of linkage – external, internal and none.
Each declared object in your program (i.e. variable or function) has some kind
of linkage – usually specified by the circumstances of the declaration.
Linkage of an object says how is the object propagated through the whole
program. Linkage can be modified by both keywords
Objects with external linkage can be seen (and accessed) through the whole program across the modules. Anything you declare at file (or global) scope has external linkage by default. All global variables and all functions have external linkage by default.
Variables and functions with internal linkage are accessible only from one compilation unit – the one they were defined in. Objects with internal linkage are private to a single module.
None linkage makes the objects completely private to the scope they were defined in. As the name suggests, no linking is done. This applies to all local variables and function parameters, that are only accessible from within the function body, nowhere else.
Another area affected by these keywords is storage duration, i.e. the lifetime of the object through the program run time. There are two types of storage duration in C – static and automatic.
Objects with static storage duration are initialized on program startup
and remain available through the whole runtime. All objects with external and
internal linkage have also static storage duration. Automatic storage
duration is default for objects with no linkage. These objects are allocated
upon entry to the block in which they were defined and removed when the
execution of the block is ended. Storage duration can be modified by the
There are two different uses of this keyword in the C language. In the first
static modifies linkage of a variable or function. The ANSI standard
If the declaration of an identifier for an object or a function has file scope and contains the storage-class specifier
static, the identifier has internal linkage.
This means if you use the
static keyword on a file level (i.e. not in a
function), it will change the object’s linkage to internal, making it private
only for the file or more precisely, compilation unit.
function() will have internal linkage and won’t be
visible from any other module.
The other use of static keyword in C is to specify storage duration. The keyword can be used to change automatic storage duration to static. A static variable inside a function is allocated only once (at program startup) and therefore it keeps its value between invocations. See the example from Stack Overflow.
The output will look like this:
The extern keyword denotes, that “this identifier is declared here, but is defined elsewhere”. In other words, you tell the compiler that some variable will be available, but its memory is allocated somewhere else. The thing is, where? Let’s have a look at the difference between declaration and definition of some object first. By declaring a variable, you say what type the variable is and what name it goes by later in your program. For instance you can do the following:
The variable virtually doesn’t exist until you define it (i.e. allocate memory for it). The definition of a variable looks like this:
You can put as many declaration as you want into your program, but only one definition within one scope. Here is an example that comes from the C standard:
This will compile without errors.
static – the storage-class specifier and static storage
duration are two different things. Storage duration is a attribute of objects
that in some cases can be modified by
static , but the keyword has multiple
extern keyword and external linkage represent two different
areas of interest. External linkage is an object attribute saying that it can
be accessed from anywhere in the program. The keyword on the other hand
denotes, that the object declared is not defined here, but someplace else.