C Defined Constants, Macros, and Conditional Compilation
There are times when we are writing our program that we would like to use certain numbers like PI, C (the speed of light), or E (the base of the natural logarithm). For example, we could just type 2.718281828 everywhere we wanted to use E, however, we would be less likely to make an error and more clear if we were to use a named constant (like E) to express what the number means. In C++ we can use the const keyword to make variables unchangable (i.e., immutable). However, in the C language that preceded C++, there was no such keyword. Rather, the C language depended on an alternative mechanism provided by the C (now C++) pre-compiler called a defined constant.
const double E = 2.718281828; // this is the C++ way of creating a constant
#define E 2.718281828 /* this is the C-style way of creating a constant */
Now while #defiine is useful for creating constants, that's not all we can do with it, because the C-style defined constant isn't really a variable like the C++ constant. The C-style #define is really just a mechanism for text substitution by the C (or C++) pre-processor. Actually, all the directives in our programs that begin with the '#' character are instructions for the pre-processor. The most common are listed below.
#include: Directs the pre-compiler to copy the referenced file at the point of reference.
#define: Used to define C-style constants and macros
The cases above are not the only pre-processor directives. They are, however, by far the most common and generally sufficient for most programming tasks. However, those that have further interest can refer to the article here.
We have already seen multiple examples in our program with the #include directive, so we will not elaborate on it further here.
See the examples defined_constants.cpp, c_macros.cpp, and conditional_compilation.cpp for examples of each of the pre-processor directives (as used with constants), #define (as used with macros) and #ifdef ... #endif (for conditional comiplation).