Learn what C++ preprocessor directives do and how they work before compilation. Explore #include, #define, and conditional directives with a simple code example.
A preprocessor directive is a special instruction in a C++ program that is processed before the actual compilation of the program.
# symbol.;).#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
Here:
#include <iostream>
is a preprocessor directive.
It tells the preprocessor to include the contents of the iostream header file before compilation.
| Directive | Purpose |
|---|---|
#include |
Includes a header file |
#define |
Defines a constant or macro |
#if |
Starts a conditional section |
#ifdef |
Checks whether a macro is defined |
#ifndef |
Checks whether a macro is not defined |
Simple definition for students:
A preprocessor directive is an instruction beginning with
#that is processed before the C++ program is compiled.