|
|
|
|
|
|
|
The compiler does not read your original source code file; it reads the output of the preprocessor and compiles that file. #include instructs the preprocessor to find the file whose name follows the #include directive and to write it into the intermediate file at that location. It is as if you had typed that entire file right into your source code, and by the time the compiler sees the source code the included file is there. |
|
|
|
|
|
|
|
|
Seeing the Intermediate Form |
|
|
|
|
|
|
|
|
Just about every compiler has a switch that instructs the compiler to save the intermediate file. You can set this switch either in the integrated development environment (IDE) or at the command line. Check your compiler manual for the right switches to set for your compiler if you'd like to examine the intermediate file. |
|
|
|
|
|
|
|
|
The #define command defines a string substitution. If you write |
|
|
|
|
|
|
|
|
you have instructed the precompiler to substitute the string 512 wherever it sees the string BIG. But this is not a string in the C++ sense. The characters 512 are substituted in your source code wherever the token BIG is seen. A token is a string of characters that can be used wherever a string or constant or other set of letters might be used. Therefore, if you write |
|
|
|
|
|
|
|
|
#define BIG 512
int myArray[BIG]; |
|
|
|
|
|
|
|
|
the intermediate file produced by the precompiler will look like this: |
|
|
|
|
|
|
|
|
Note that the #define statement is gone. Precompiler statements are all removed from the intermediate file; they do not appear in the final source code at all. |
|
|
|
|
|
|
|
|
Using #define for Constants. |
|
|
|
|
|
|
|
|
One way to use #define is as a substitute for constants. This is almost never a good idea, however, because #define merely makes a string substitution and does no type checking. There are tremendous advantages to using the const keyword rather than #define. |
|
|
|
|
|