The extern keyword has to do with linkage. That is, whether or not the variable is sharable outside the current scope.
A static global variable is not sharable outside the current scope, typically the current file. It is accessible only from the point of definition to the end of the file.
An extern variable is sharable outside the current scope. In the case of the exanple variable, the variable is defined and a value placed in it. The variable is sharable (accessible) from other source files:
However, if there is no value, then the variable does not need to bed defined. In this case, the extern means there is a sharable variable somewhere else:
- extern int example; //no variable created
const global variables have internal linkage (static) and so are not sharable with other source files. However, this is the default behavior. You can create a sharable const variable by defining using the extern keyword:
- extern const int example = 0;
and this variable is accessible from another source file by:
- extern const int example;