Sams Teach Yourself C++ in 21 Days

(singke) #1

The Preprocessor and the Compiler ....................................................................


Every time you run your compiler, your preprocessor runs first. The preprocessor looks
for preprocessor instructions, each of which begins with a pound symbol (#). The effect
of each of these instructions is a change to the text of the source code. The result is a
new source code file—a temporary file that you normally don’t see, but that you can
instruct the compiler to save so you can examine it if you want to.
The compiler does not read your original source code file; it reads the output of the pre-
processor and compiles that file. You’ve seen the effect of this already with the #include
directive. This instructs the preprocessor to find the file whose name follows the
#includedirective 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.

752 Day 21


Nearly every compiler has a switch that you can set either in the Integrated
Development Environment (IDE) or at the command line, which instructs the
compiler to save the intermediate file. Check your compiler manual for the
right switches to set for your compiler if you want to examine this file.

TIP

The #definePreprocessor Directive....................................................................


You can create string substitutions using the #definecommand you write
#define BIG 512
you have instructed the precompiler to substitute the string 512 wherever it sees the
string BIG. This is not a string in the C++ sense. The characters “512” are substituted in
your source code wherever the word “BIG” is seen. Thus, if you write
#define BIG 512
int myArray[BIG];
the intermediate file produced by the precompiler looks like this:
int myArray[512];
Note that the #definestatement is gone. Precompiler statements are all removed from
the intermediate file; they do not appear in the final source code at all.
Free download pdf