🧠 C++ Preprocessor – Do Things Before Compilation Starts
The preprocessor in C++ runs before your code is actually compiled. Think of it as a helper that sets the stage — like arranging the props before a play begins! 🎭
It handles special instructions called directives that begin with a # symbol. These are not C++ statements — they’re commands to the compiler.
📌 Common Preprocessor Directives
#include– includes another file (like libraries)#define– defines constants or macros#undef– undefines something previously defined#ifdef,#ifndef– conditionally compile parts of the code#if,#else,#elif,#endif– more conditional checks
🔧 Example: #include and #define
#include <iostream>
#define PI 3.14159
using namespace std;
int main() {
cout << "Value of PI: " << PI << endl;
return 0;
}
🧪 Example: Conditional Compilation
This helps when your code depends on certain conditions.
#include <iostream>
#define DEBUG_MODE
using namespace std;
int main() {
#ifdef DEBUG_MODE
cout << "Debug mode is ON" << endl;
#endif
cout << "Program is running!" << endl;
return 0;
}
🎯 Summary
- Preprocessor prepares your code before it’s compiled
- Use
#includeto add files #defineto create constants or macros- Conditional directives make your code smarter!