Preprocessor Introduction

The 'C' Preprocessor is more a part of the compiler than part of the language. That is, it can be used separately from translation of a 'C' source language file to machine language.

The preprocessor is a text processor. The 'C' compiler usually gives a way (typically via a command line switch) to take a source or input file, operate on it with the preprocesssor, and then produce an output file, without compiling the output of the preprocessor. Normally, to save time when compiling, this is done with a 'C' source file before the lexical phase of the compiler in such a way so as to not need an intermediate file.

The preprocessor has its own syntax consisting of operators and directives. They operate on tokens (see: "Lexical Rules" - from main menu). The preprocessor is a most valuable addition to the compiler. The preprocessor contributes clarity to a program, makes it easier to make changes to a program and gives a flexibility that would be very hard to achieve in any other way.

All preprocessor directives start with a '#' character, which must be the first non white space character in a line. Older preprocessors were more restrictive and required the '#' character to be the first character in a line for the preprocessor directive to be recognized. White space can also appear after the '#' and before the first letter of the directive.

Preprocessor directives can appear anywhere in a source file, but they will only affect the remainder of that source file.

The 11 'C' preprocessor directives are:
     #include   #define   #ifdef    #else   #if     #line
                #undef    #ifndef   #endif  #elif   #pragma

There are three preprocessor operators. They are only recognized within a preprocessor directive. They are the stringizing operator, '#', the token-pasting operator '##', and the "defined()" operator.

Normally a preprocessor directive must be completely contained on one line. The newline in the source code at the end of the line terminates the directive. This is not always convenient, so a means is given to allow a preprocessor directive to continue on to the next line. The method is to use the backslash character, '\\', followed immediately by a newline in the source file. This can, in principle, be done any number of times, with the last line not having the backslash-newline sequence to terminate the directive. In actual practice, all compilers will have some limit in terms of the maximum number of characters that a single preprocessor directive can have. If the limit is exceeded, the compiler will tell you.


© 1991-2008 Prem Sobel. All Rights Reserved.