Preprocesor Directives
Preprocessor directives are statements and commands that control the compilation of the source code or perform other compile-time operations on the code. Preprocessor directives do not affect the actual runtime logic of the program, but rather instruct the compiler how to handle various parts of the code. These special instructions begin with a # and can be used to perform the following actions:
  1. Embed one file within another
  2. Simplify the code using macros
  3. Conditionally compile the code
Embedding Files
Embedding files within other files is accomplished with the #include preprocessor directive. This directive instructs the compiler to insert the contents of the specified file at the location of the statement. This is how classes and other utilities located in separate files are incorporated into a single program for utilization. There are two ways of using the #include directive which vary only in the characters that are used to encapsulate the filename of the file to include:
#include <file>
Using the above method, the specified file, located in the compiler library directory, will be inserted into the file at the location of the statement. If the specified file does not exist in the compiler library directory, an error will be generated. This method should be used to include libraries that are a part of the standard or added C++ libraries.
#include "file"
Using this method, the specified file, which is either located in the current directory or the directory specified in the full path and filename specified, is inserted into the file at the location of the statement. This method is suitable for including user-developed libraries that are not a part of the standard C++ libraries.
Macros
Macros are used by the compiler to perform a simple search and replace for two specified values. When the program code is actually compiled, the compiler will replace all of the specified source strings with the specified target strings. Macros are defined using the #define directive, and take the following form:
#define SOURCE TARGET
For example, macros can be used to centralize certain variables that do not need the error-checking of ordinary variables, such as static array sizes or other numerical constants. The following are examples:
#define ARRAY_LENGTH 100
#define PI 3.14159265359
In the above examples, whenever the compiler encounters the source tag, say, ARRAY_LENGTH, it replaces it with the target value - in this case, 100. This transforms the typical declaration char myArray[ARRAY_LENGTH] into char myArray[100]. These macros can be used anywhere in a program, such as in loops, dclarations, or anywhere else, and can even be used to define which function call should be executed at a particular location. In essence, macros allow the centralized location of information that varies in a program, but does not require error-checking.
Conditional Compilation
It is possible to indicate which parts of the course code of a program should be compiled using conditional preprocessor directives. These directives are #ifdef (if undefined), and #ifndef (if undefined). Each directive takes, as a parameter, a variable or constant that determines whether or not the statement executes. Take the following example:
#ifndef FILE_H
#define FILE_H
...Some Code...
#endif
The above example conditionally includes all the code or executes all the preprocessor directives contained between the conditional statement and the closing #endif. The closing tag is required to properly denote the end of the conditional code segment. If the conditional expression evaluates to false, then all the code or directives in the conditional segment areeffectively deleted when the program is compiled.
On This Page...
Embedding Files
Macros
Conditional Compilation