External variable

(This mechanism is rather like Fortran COMMON or Pascal variables declared in the outermost block.)

Because external variables are globally accessible, they can be used instead of argument lists to communicate data between functions.

If neither the extern keyword nor an initialization value are present, the statement can be either a declaration or a definition.

In this case the extern keyword must be used, otherwise the compiler will consider it a definition of a local (automatic) variable, which has a different scope, lifetime and initial value.

... You should note that we are using the words definition and declaration carefully when we refer to external variables in this section.

Definition refers to the place where the variable is created or assigned storage; declaration refers to places where the nature of the variable is stated but no storage is allocated.

An external variable can be accessed by all the functions in all the modules of a program.

The static keyword (static and extern are mutually exclusive), applied to the definition of an external variable, changes this a bit: the variable can only be accessed by the functions in the same module where it was defined.

In this case, even though the function is in another module, it can read and modify the contents of the variable—it just cannot refer to it by name.

The variable is still local, since it can only be accessed by name inside the function that defined it.

The usual practice is to collect extern declarations of variables and functions in a separate file, historically called a header, that is included by #include at the front of each source file.

The normal methodology is for allocation and actual definitions to go into .c files, but mere declarations and prototypes do not allocate and just describe the types and parameters so that the compiler can work correctly, and that information belongs in a .h header file that others can safely include without any possible conflict.