4.3 The ‘C’ Preprocessor

If global declarations must be repeated in full in each file (as is required by Fortran, for instance) then the bulk of the program is increased, and modifying a declaration is at best a nuisance, and at worst, highly error-prone.

These difficulties are avoided in UNIX by use of the preprocessor facility of the “C” compiler. This allows declarations for most global variables to be recorded once only in one of the few “h” files.

Whenever the declaration for a particular global variable is required the appropriate “h” file can then be “included” in the file being compiled.

UNIX also uses the “h” files as vehicles for lists of standard definitions for many symbolic names which represent constants and adjustable parameters, and for declaration of some structure types.

For example, if the file bottle.c contains a procedure “glug” which global variable called “gin” which is declared in the file “box.h” then a statement:

        #include "box.h"

must be inserted at the beginning of the file “bottle.c” When the file “bottle.c” is compiled, all declarations in “box.h” are compiled, and since they are found before the beginning of any procedure in “bottle.c” they are flagged as external in the relocatable module which is produced.

When all the object modules are linked together, a reference to “gin” will be found in every file for which the source included “box.h” All these references will be consistent and the loader will allocate a single space for “gin” and adjust all the references accordingly.