CSC195, Class 47: The C Preprocessor Overview: * About the C Preprocessor * What really happens with includes * Use of #define + Constants + Macros + More * Some silly tricks Notes: * No more homework! * Exam coming soon, though The C preprocessor is the part of the compiler that goes through and does "the easy stuff". * Might strip many of your comments. * Lots of fun with # signs #include #define #ifdef What does #include do? * Literally includes the file that you just named right there in the code. * Processes it recursively. * Be careful about recursion. Note that the fact that it is including the file is important. For example, suppose you * put a function *definition* in a header * include that header in two files * compile separate * link Result: function **** defined twice. Are you sure you're a real C programmer? Most compilers let you get away with some things you shouldn't be able to get away with. E.g., function declarations in headers should be declared *extern* The same holds for global variables -------- #define NAME val A constant Textual substitution #define FRED x+y Macros (see examples likarish.h and martinsa.c) #ifdef NAME code-to-use-if-defined #endif #ifndef NAME code-to-use-if-not-defined #endif #ifdef NAME ... #else ... #endif Example: Different code for different architectures #ifdef MC68000 #include #else #include #endif Where would MC68000 be defined? (1) In an included "mysystem.h" or "config.h" (2) Compile-time flag cc -DMC68000 Getting trickier: * Building new names on the fly STUFF ## PARAM Why? * compile-time string-append * wicked-neat polymorphism