C Preprocessor
- Before your program gets compiled, it actually runs through a phase called preprocessing. We’ve already seen this to an extent with #include! That’s the C Preprocessor! Where it sees that directive, it includes the named file right there, just as if you’d typed it in there. And then the compiler builds the whole thing.
Macros
Simple Macros
Conditional Compilation
Built-in macros
Macros with arguments
- One argument
Operator #
- We can turn any argument into a string by preceding it with a # in the replacement text.
Operator ##
- We can concatenate 2 arguments together with
##
:
Macros with a variable number of arguments
The func identifier
- The
__func__
identifier is a string variable that stores the name of the currently executing function => makes it possible to write debugging macros such as the following:
Conditional compilation
#if and #endif
#defined operator
- Equivalent to
#error directive
- This directive causes the compiler to error out as soon as it sees it. Commonly, this is used inside a conditional to prevent compilation unless some prerequisites are met
Uses of conditional compilation
- Writing programs that are portable to several machines or operating systems.
- Writing programs that can be compiled with different compilers
- Provide a default definition for a macro
- **Protecting header files against multiple inclusion.