C PreProcessor Directives

The preprocessor for C or cpp is the macro preprocessor used for the C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.

One of the most common uses of the preprocessor is to include another file. In many C implementations, it is a separate program invoked by the compiler as the first part of translation.

The language of preprocessor directives is only weakly related to the grammar of C, and so is sometimes used to process other kinds of text files.

The C preprocessor modifies a source file before handing it over to the compiler, allowing conditional compilation with #ifdef, defining constants with #define, including header files with #include, and using builtin macros such as __FILE__. The preprocessor directives, or commands to the preprocessor, that are available include:

#include #define #undef #if #ifdef #ifndef #error __FILE__ __LINE__ __DATE__ __TIME__ __TIMESTAMP__ pragma # macro operator ## macro operator

#if, #ifdef, #ifndef, #else, #elif and #endif can be used for conditional compilation.

There are two types of macros, object-like and function-like. Object-like macros do not take parameters; function-like macros do. The generic syntax for declaring an identifier as a macro of each type is, respectively:


 * 1) define                   // object-like macro
 * 2) define // function-like macro, note parameters

The function-like macro declaration must not have any whitespace between the identifier and the first, opening, parenthesis. If whitespace is present, the macro will be interpreted as object-like with everything starting from the first parenthesis added to the token list.

A macro definition can be removed with "#undef":


 * 1) undef                                             // delete the macro