C++ Preprocessor Directives, Conditional Compilation & Macro Pitfalls
Understand #define object/function macros, conditional compilation (#ifdef, #ifndef, #if), predefined macros (__LINE__, __FILE__), and why constexpr/inline beat macros.
- Understand #define object/function macros, conditional compilation (#ifdef, #ifndef, #if), predefined macros (__LINE__, __FILE__), and why constexpr/inline beat macros.
- Practice writing and running standard examples.
- Apply best practices and debugging techniques.
1. Preprocessor Directives & Conditional Compilation
The preprocessor runs before compilation: #include, #define, #undef, #ifdef, #ifndef, #if, #elif, #else, #endif. Predefined macros: __FILE__, __LINE__, __DATE__, __TIME__, __cplusplus.
2. Macro Pitfalls vs. Modern `constexpr` / `inline`
Function-like macros (#define SQR(x) x*x) lack type checking and evaluate arguments multiple times (SQR(i++) increments i twice!). Replace constant macros with constexpr and function macros with constexpr / inline functions.
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)