The C Preprocessor :: Overview
- 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
#. This listing is from Weiss pg. 104. The unconditional directives are:#include- Inserts a particular header from another file#define- Defines a preprocessor macro#undef- Undefines a preprocessor macro
#ifdef- If this macro is defined#ifndef- If this macro is not defined#if- Test if a compile time condition is true#else- The alternative for #if#elif- #else an #if in one statement#endif- End preprocessor conditional
#- Stringization, replaces a macro parameter with a string constant##- Token merge, creates a single token from two adjacent ones
- Some examples of the above:
#define MAX_ARRAY_LENGTH 20
Tells the CPP to replace instances of MAX_ARRAY_LENGTH with 20. Use#definefor constants to increase readability. Notice the absence of the;.#include
Tells the CPP to get stdio.h from System Libraries and add the text to this file. The next line tells CPP to get mystring.h from the local directory and add the text to the file. This is a difference you must take note of.
#include "mystring.h"#undef MEANING_OF_LIFE
Tells the CPP to undefine MEANING_OF_LIFE and define it for 42.
#define MEANING_OF_LIFE 42#ifndef IROCK
Tells the CPP to define IROCK only if IROCK isn't defined already.
#define IROCK "You wish!"
#endif#ifdef DEBUG
Tells the CPP to do the following statements if DEBUG is defined. This is useful if you pass the
/* Your debugging statements here */
#endif-DDEBUGflag to gcc. This will define DEBUG, so you can turn debugging on and off on the fly!