C General 17
C General 17
The C Preprocessor is not part of the compiler, but is a separate step in the compilation process. In simplistic terms, a C Preprocessor is just a text substitution tool. We'll refer to the C Preprocessor as the CPP. All preprocessor lines begin with #
The unconditional directives are: o #include - Inserts a particular header from another file o #define - Defines a preprocessor macro o #undef - Undefines a preprocessor macro The conditional directives are: o #ifdef - If this macro is defined o #ifndef - If this macro is not defined o #if - Test if a compile time condition is true o #else - The alternative for #if o #elif - #else an #if in one statement o #endif - End preprocessor conditional Other directives include: o # - Stringization, replaces a macro parameter with a string constant o ## - Token merge, creates a single token from two adjacent ones
Pre-Processors Examples:
Analyze following examples to understand various directives.
#define MAX_ARRAY_LENGTH 20
Tells the CPP to replace instances of MAX_ARRAY_LENGTH with 20. Use #define for constants to increase readability.
Stringize (#):
The stringize or number-sign operator ('#'), when used within a macro definition, converts a macro parameter into a string constant. This operator may be used only in a macro that has a specified argument or parameter
list. When the stringize operator immediately precedes the name of one of the macro parameters, the parameter passed to the macro is enclosed within quotation marks and is treated as a string literal. For example:
#include <stdio.h> #define message_for(a, b) \ printf(#a " and " #b ": We love you!\n")
Parameterized Macros:
One of the powerful functions of the CPP is the ability to simulate functions using parameterized macros. For example, we might have some code to square a number:
Macro Caveats:
Macro definitions are not stored in the object file. They are only active for the duration of a single source file starting when they are defined and ending when they are undefined (using #undef), redefined, or when the end of the source file is found. Macro definitions you wish to use in multiple source files may be defined in an include file which may be included in each source file where the macros are required. When a macro with arguments is invoked, the macro processor substitutes the arguments into the macro body and then processes the results again for additional macro calls. This makes it possible, but confusing, to piece together a macro call from the macro body and from the macro arguments. Most experienced C programmers enclose macro arguments in parentheses when they are used in the macro body. This technique prevents undesired grouping of compound expressions used as arguments and helps avoid operator precedence rules overriding the intended meaning of a macro. While a macro may contain references to other macros, references to itself are not expanded. Selfreferencing macros are a special feature of ANSI Standard C in that the self-reference is not interpreted as a macro call. This special rule also applies to indirectly self-referencing macros (or macros that reference themselves through another macro).