C++ Preprocessor And Preprocessor Directives
Last Updated :
05 Apr, 2025
The preprocessor in C++ is a tool that process the code before it is compiled by the compiler. It does many tasks such as including header files, conditional compilation, text substitution, removing comments, etc. Preprocessor also allows the developers to select which portions of code should be included or excluded.
The code processed by the preprocessor is called expanded code and is generally saved with a ".i" file extension.

Preprocessor Directives in C++
In C++, the preprocessor directives are special commands that are used to instruct the preprocessor. They begin with a '#' symbol and tells the preprocessor to the modify source code before compilation. There are different preprocessor directives in C++ for different operations.
The below table lists frequently used preprocessor directives:
Directive | Description |
---|
#include | Links a header file in the source code. |
---|
#define | Creates a symbolic or macro constant. |
---|
#undef | Deletes a macro that has already been defined. |
---|
#if / #elif / #else / #endif | Compilation that is conditional based on some expression. |
---|
#ifdef / #ifndef | Compilation that is conditional on the presence or absence of a macro. |
---|
#error | Halts the compilation process and produces an error notice. |
---|
#warning | During compilation, a warning notice is shown. |
---|
#pragma | Provide the compiler specific instructions. |
---|
1. #include
The #include preprocessor directive is used to include the contents of one file into the current one, use the #include directive. Header files are often included using this directive.
Syntax
C++
#include <file_name>
#include "file_name"
The first syntax includes the files from the system directory, whereas the second one includes the file from directory the source file is currently in.
Example
C++
// Including the standard I/O stream header file
#include <iostream>
using namespace std;
int main() {
cout << "GeeksforGeeks";
return 0;
}
2. #define
The #define preprocessor directive is used to define macros. Macro names are symbolic and may be used to represent constant values or short bits of code. Using #define preprocessor makes our code more readable and easily maintainable as we can replace numbers and code snippets with a meaningful name.
Syntax
C++
Example
C++
#include <iostream>
using namespace std;
// Defining macros
#define PI 3.14159
#define findSquare(x) (x * x)
int main() {
double radius = 5.0;
// Macro name PI and findSquare will be substituted
// by preprocessor
double area = PI * findSquare(radius);
cout << area;
return 0;
}
3. #undef Directive
The #undef preprocessor directive is used to undefined a previously defined macro (defined using #define). It is mainly used in the case when we want to redefine an existing macro or eliminate a macro definition associated with it from the code.
Syntax
C++
Example
C++
#include <iostream>
using namespace std;
// Defining a macro
#define MAX_VALUE 100
int main() {
cout << MAX_VALUE << endl;
// using undef to change MAX_VALUE
#undef MAX_VALUE
#define MAX_VALUE 200
cout << MAX_VALUE;
return 0;
}
4. #if, #elif, #else, and #endif Directives (Conditional Directives)
The #if, #elif, #else, #endif, and #error are conditional preprocessor directives these are used for conditional compilation. These are used to include or exclude a code based on some conditions specified.
Syntax
C++
#if constant_expr
// Code to be executed if constant_expression is true
#elif another_constant_expr
// Code to be excuted if another_constant_expression is true
#else
// Code to be excuted if none of the above conditions are true
#endif
Example
C++
#include <iostream>
using namespace std;
#define PI 3.14159
int main() {
// Conditional compilation
#if defined(PI)
cout << "PI is defined";
#elif defined(SQUARE)
cout << "PI is not defined";
#else
#error "Neither PI nor SQUARE is defined"
#endif
return 0;
}
Explanation: This code uses preprocessor directives to check whether certain macros (PI and SQUARE) are defined. Since PI is defined, the program prints “PI is defined”, then checks if SQUARE is defined which is not so it doesn't print anything.
5. #ifdef and #ifndef
The #ifdef and #ifndef are preprocessor directives that are used for conditional compilation. #ifndef verifies that a macro is not defined, #ifdef verifies that a macro is defined.
Syntax
C++
#ifdef macro_name
// Code to be executed if macro_name is defined
#ifndef macro_name
// Code to be executed if macro_name is not defined
#endif
#ifdef and #ifndef are often used with the #endif directive to include or exclude portions of code based on whether a certain macro is defined or not.
Example
C++
#include <iostream>
using namespace std;
// Definign two macros
#define DEBUG
#define PI 3.14
int main() {
// Check if DEBUG is defined
#ifdef DEBUG
cout << "Debug mode is ON" << endl;
#else
cout << "Debug mode is OFF" << endl;
#endif
// Check if PI is defined
#ifndef PI
cout << "PI is not defined" << endl;
#else
cout << "PI is defined" << endl;
#endif
return 0;
}
OutputDebug mode is ON
PI is defined
6. #error
The #error directive is a preprocessor directive that is used to print a custom error message for compilation error. If any condition is not met or any particular requirement is not satisfied we can stop the compilation process using #error.
Syntax
C++
Here, error_message is the custom error message that you want to print when the #error is encountered.
Example
C++
#include <iostream>
using namespace std;
// not defining PI here
// #define PI 3.14159
int main() {
#if defined(PI)
cout << "PI is defined" << endl;
#else
#error "Neither PI nor SQUARE is defined"
#endif
return 0;
}
Output
#error "Neither PI nor SQUARE is defined"
7. #warning
The #warning preprocessor directive is used to generate a warning message during compilation. We can write custom warning messages generally used for any informational or debugging purpose. The compiler prints the warning message in console, to give an alert about any required condition or decisions that are made during the preprocessing stage.
Syntax
#warning message
Here, the message is any custom message that you want to print as an alert.
Example
C++
#include <iostream>
using namespace std;
// not defining it to trigger the warning
//#define PI 3.14
#ifndef PI
#warning "PI is not defined!"
#endif
int main() {
cout << "Hey! geek";
return 0;
}
Output
main.cpp:8:2: warning: #warning "PI is not defined!" [-Wcpp]
8 | #warning "PI is not defined!"
| ^~~~~~~
Hey! geek
Note: The warning message is printed when the code is compiled to the compiler output or console. It is compiler dependent. Hence, how the warning is displayed depends on the compiler you are using.
8. #pragma
The #pragma directive is a compiler-specific instructions. Special instructions to the compiler are provided using the #pragma directive. It may be used to alter compiler parameters, silence warnings behaviour, etc. Supported pragmas can vary between different compilers as they are compiler specific.
Syntax
C++
Commonly Used #pragma Flags
- #pragma once: used to include guard for header files.
- #pragma message: used to print custom messages at the time of compilation.
- #pragma warning: used to control warning behaviour (like enable or disable warnings).
- #pragma optimize: used to control optimization settings (manage optimization level).
- #pragma comment: used to include some additional information in the object file (or specify linker options).
Example
C++
#include <iostream>
using namespace std;
#pragma once
// Defining PI to trigger a pragma message during
// compilation
#define PI 3.14
// to set aggressive optimization level
#pragma optimize("O3")
int main() {
#ifdef PI
#pragma message("YES! PI is defined.")
#endif
cout << "In main function!\n";
return 0;
}
Output
./Solution.cpp: In function 'int main()':
./Solution.cpp:15:38: note: #pragma message: YES! PI is defined.
#pragma message("YES! PI is defined.")
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read