Any line that starts with a # is a preprocessor directive.
The preprocessor goes through your code first and makes any changes called for by those lines starting with #.
Suppose you have in your code a value, say 1.25, that is used in hundreds of functions in hundreds of source files. Now your boss says the rate is changed to 2.0 and you have to make hundreds of changes in those sources file without screwing up and changing a 1.25 to 2 where the 1.25 was something else and needed to be left alone.
Improvement #1:
At the beginning of each file you code:
Now you change the 1.25 in those functions to RATE.
When you compile the preprocessor scans the code and changes all RATE to 2.0 and creates a new source file with 2.0 in it. This file is the one the compiler compiles and it is called a
translation file.
To change the rate to 2.0 all you have to do is change the #define in each file to:
Of course, you have to do this in each of the hundreds of source files.
Improvement #2:
Write a header file with the #define in it.
- /*MyHeader.h */
-
-
#define RATE 2.0
-
Now go to each source file ad remove the #define and replace it with:
The first time you need to change each source file but after that to change the rate to 3.0 all you have to do is change the #define in the header file to 3.0 and then recompile your code and you are done.
#include inserts a copy of MyHeader.h in the translation file right where the #include appears in the original code. The #include is then commented out by the preprocessr so it doesn't cause a compile time error.
Experienced developers start off with a #include <MyHeader.h> in each source file to avoid all the rigmarole of not having it.