pre-processor
pre-processor
of the program begins. They are commonly used in languages like C and C++ to handle tasks such as file
inclusion, macro definitions, and conditional compilation. Preprocessor directives typically start with the
# symbol and do not require a semicolon at the end.
1. #include
This directive is used to include the contents of another file, usually a header file, into the current file.
2. #define
Used to define macros, which are constants or small functions that are substituted into the code during
preprocessing.
3. #undef
This directive is used to undefine a macro that has been previously defined using #define.
#undef PI
These directives are used for conditional compilation. They allow certain parts of the code to be
compiled or ignored based on whether certain conditions are met (such as whether a macro is defined).
#ifdef DEBUG
printf("Debugging enabled.\n");
#endif
#ifndef VERSION
#define VERSION 1
#endif
#if VERSION == 1
printf("Version 1.\n");
#elif VERSION == 2
printf("Version 2.\n");
#else
printf("Unknown version.\n");
#endif
5. #pragma
This directive provides additional information to the compiler, such as disabling certain warnings
or optimizing specific sections of code. Different compilers may support different #pragma directives.
6. #error
This directive forces the compiler to produce an error message if a certain condition is met. It’s useful for
stopping compilation when an essential condition is not met.
#ifndef PI
#endif
7. #line
This directive is used to change the line number and the filename reported by the compiler, which can
be useful for debugging.
Preprocessor directives are executed before the compiler starts compiling the actual code, which makes
them very useful for setting up the environment, including files, and conditionally compiling code.