1️⃣ What is a Preprocessor Directive?
A preprocessor directive in C++ is a command that starts with #
and is executed before compilation. It helps with:
✔ Including header files (#include
)
✔ Defining constants (#define
)
✔ Conditional compilation (#ifdef
, #ifndef
, #endif
)
✔ Macros and inline code substitution
⏩ Preprocessor directives do NOT end with a semicolon (;
).
2️⃣ Common Preprocessor Directives in C++
Directive | Purpose |
---|---|
#include |
Includes a file (like <iostream> , user-defined headers). |
#define |
Defines macros/constants. |
#undef |
Undefines a macro. |
#ifdef |
Checks if a macro is defined. |
#ifndef |
Checks if a macro is not defined. |
#if , #elif , #else , #endif |
Conditional compilation. |
#pragma |
Compiler-specific instructions. |
3️⃣ #include
: Including Header Files
The #include
directive is used to import external files (standard libraries or user-defined headers).
🔹 Example: Including Standard Library
#include <iostream> // Includes input-output stream
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
✅ #include <iostream>
allows the use of cout
, cin
, etc.
🔹 Example: Including a User-Defined Header File
File: myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H
void greet() {
std::cout << "Hello from My Header!" << std::endl;
}
#endif
File: main.cpp
#include <iostream>
#include "myheader.h" // Includes user-defined header
using namespace std;
int main() {
greet();
return 0;
}
🔹 Output
Hello from My Header!
✅ Using #ifndef
prevents multiple inclusions of myheader.h
.
4️⃣ #define
: Creating Macros & Constants
The #define
directive is used to define constants or macros.
🔹 Example: Define a Constant
#include <iostream>
#define PI 3.14159
int main() {
cout << "Value of PI: " << PI << endl;
return 0;
}
🔹 Output
Value of PI: 3.14159
✅ Replaces PI
with 3.14159
before compilation.
🔹 Example: Define a Macro Function
#include <iostream>
#define SQUARE(x) (x * x) // Macro function
int main() {
cout << "Square of 5: " << SQUARE(5) << endl;
return 0;
}
🔹 Output
Square of 5: 25
✅ Replaces SQUARE(5)
with (5 * 5)
before compilation.
5️⃣ Conditional Compilation: #ifdef
, #ifndef
, #endif
🔹 #ifdef
: Check if Macro is Defined
#include <iostream>
#define DEBUG
int main() {
#ifdef DEBUG
std::cout << "Debug Mode is ON" << std::endl;
#endif
return 0;
}
🔹 Output
Debug Mode is ON
✅ Since DEBUG
is defined, the cout
statement executes.
🔹 #ifndef
: Check if Macro is NOT Defined
#include <iostream>
#ifndef VERSION
#define VERSION 1.0
#endif
int main() {
std::cout << "Software Version: " << VERSION << std::endl;
return 0;
}
🔹 Output
Software Version: 1.0
✅ Defines VERSION
only if it was not already defined.
6️⃣ #if
, #elif
, #else
, #endif
: Conditional Compilation
#include <iostream>
#define MODE 2 // Change this value to test
int main() {
#if MODE == 1
std::cout << "Mode 1 Selected" << std::endl;
#elif MODE == 2
std::cout << "Mode 2 Selected" << std::endl;
#else
std::cout << "Invalid Mode" << std::endl;
#endif
return 0;
}
🔹 Output
Mode 2 Selected
✅ Compiles different code based on MODE
.
7️⃣ #pragma
: Compiler-Specific Directives
🔹 #pragma once
: Prevent Multiple Inclusions
Instead of using #ifndef
guards, you can use:
#pragma once
void greet() {
std::cout << "Hello from Pragma!" << std::endl;
}
✅ Ensures the file is included only once.
8️⃣ Summary
✔ #include
→ Includes standard/user-defined files.
✔ #define
→ Defines constants and macros.
✔ #ifdef
, #ifndef
→ Checks if a macro is defined or not.
✔ #if
, #elif
, #else
, #endif
→ Conditional compilation.
✔ #pragma
→ Compiler-specific instructions.
Would you like a real-world project using preprocessor directives? 🚀
0 Comments