13.1 The Preprocessor

The C++ preprocessor is a text processor that manipulates the text of a source file as part of the first phase of translation. (For more information, see Appendix A, “Phases of Translation.”) The preprocessor does not parse the source text, but it does break it up into tokens for the purpose of locating macro calls. Although the compiler ordinarily invokes the preprocessor in its first pass, the preprocessor can also be invoked separately to process text without compiling.

Microsoft Specific

You can obtain a listing of your source code after preprocessing by using the /E or /EP compilation options. Both options cause the preprocessor to be invoked and the resulting text to be output to the standard output device which, in most cases, is the console. The difference between the two options is that /E includes #line directives, and /EP strips these directives out.¨

Preprocessor directives are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to perform specific actions. For example, the preprocessor can replace tokens in the text, insert the contents of other files into the source file, or suppress compilation of part of the file by removing sections of text. Preprocessor lines are recognized and carried out before macro expansion. Therefore, if a macro expands into something that looks like a preprocessor command, that command is not recognized by the preprocessor.

Preprocessor statements use the same character set as source file statements with the exception that escape sequences are not supported. The character set used in preprocessor statements is the same as the execution character set. (For information on the execution character set, see “Character Constants” in Chapter 1, on topic .) The preprocessor also recognizes negative character values.

The C++ preprocessor recognizes the following directives:@TABLE TEXT = #define@TABLE TEXT = #elif@TABLE TEXT = #else@TABLE TEXT = #endif@TABLE TEXT = #error@TABLE TEXT = #if@TABLE TEXT = #ifdef@TABLE TEXT = #ifndef@TABLE TEXT = #include@TABLE TEXT = #line@TABLE TEXT = #pragma@TABLE TEXT = #undef

The number sign (#) must be the first nonwhite-space character on the line containing the directive; white-space characters can appear between the number sign and the first letter of the directive. Some directives include arguments or values. Any text that follows a directive (except an argument or value that is part of the directive) must be preceded by the single-line comment delimiter (//) or enclosed in comment delimiters (/* */). Lines containing preprocessor directives can be continued by immediately preceding the end-of-line marker with a backslash (\).

Preprocessor directives can appear anywhere in a source file, but they apply only to the remainder of the source file.