19 - The Preprocessor
How do you control what source code gets compiled?
#if (conditional) code(); #endif
How can you use macros to help with debugging?
#if defined(DEBUG) // debug code #else // debug macros expand to nothing #endif
What is the preprocessor directive for asking if macro X is defined?
#ifdef X
How do you stop the same header from being included more than once?
#ifndef C_H #define C_H // All of the stuff in c.h #endif
How do you put a macro variable in quotes after expansion?
#variableName
How do you choose which macros to enable when compiling?
-DMACRO Sets the value of MACRO to 1 Alternatively, to give it a value: -DMACRO=5 Or put the whole thing in quotes to include spaces in expansion
What is this called? #define SIZE 256 What is it called when it is replaced in the code?
A preprocessor macro A macro expansion
Macros use call-by-_________ What is the benefit of this?
Name We can make macros change variables directly without needing a pointer
What are all commands at the beginning of a program starting with # called?
Preprocessor directives
How do you safely write a macro definition?
Put parentheses around each instance of a variable and around the entire definition
If MAX is a macro, why is calling MAX(x++, y++) a bad idea?
The variables may get incremented multiple times
Can you nest conditional compilation?
Yes
If you have this code: #if defined(LINUX) #define HDR "linux.h" #elif defined(WIN32) #define HDR "windows.h" #else #define HDR "default.h" #endif #include HDR How would you compile the program to include "windows.h"?
gcc -DWIN32 myprog.c
How would you compile myProgram.c to send preprocessed output to standard out?
gcc -E myProgram.c
How do you concatenate two macro variables after expansion?
var1##var2