UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

14.11.  The Preprocessor

The preprocessor is a program that is part of the compiler and prepares or modifies the source code before it is translated to binary code. The changes are done by interpreting those lines starting with the # symbol. The use of these directives are so common in C programs that they appear to be part of the language. But in fact, they are part of a language that is only understood by the processor. The following figure shows how a file with source code is really processed by the compiler.

As it can be seen, the two lines starting with # have disappeared, as well as the comments. The translator to binary code receives a file with no directives or comments.

The preprocessor can be used independently from the compiler using the cpp command. Open a command terminal and check the manual page for this command. As you can see, the program can process a large catalog of directives. Out of all of them, in this document only the most frequently used are described.

14.11.1.  The #include directive

The #include directive must be followed by the name of a file and its effect is to replace this line with the source code in contained in the specified file. The following figure shows an example of this directive.

With this directive you must take into account the following:

  • If the file that follows the #include is surrounded by < and >, the preprocessor searches for it in the internal system directories. This version is then used to include files given by the system.

  • If the file is surrounded by double quotes, the preprocessor searches the directory where the compilation is taking place. This version of the directive is used to include files written by the user. The -L option in the compiler allows to specify a list of additional directories where to search for files to include.

  • The extension .h is typically used for the files that are included in programs with this directive.

  • Before including the file content, the directives that contains are processed. This allows for a file included in a program to include itself other files.

  • This type of file typically contain definitions that are required in more than one file. Instead of repeating these definitions in the source code files (with .c extension), they are moved to a file with extension .h that is then included in all of them.

  • The files included with the #include directive are not included in the command invoking the compiler. Its use is decided by the preprocessor when the #include directive is found.