Preprocessor directives are one of the unique features of C++. It provides many tools that other high-level language does not, and programmers can use these tools to create efficient, easy-to-read, easy-to-modify and portable C ++ programs.
Before a C++ program gets compiled by the compiler, the source-code gets processed by the compiler. This technique is called preprocessor and the process is called preprocessing. It is separate program part that the C++ compiler invokes it at the first part of the translation. This technique is not a part of the compiler but it is a separate method that comes under compilation process. It directs the compiler that the information should be preprocessed before the actual compilation starts.
All preprocessor directives in C++ begin with #, and they do not need to end with a semicolon(;) because this is not a statement in C++.
The #define directive creates a symbolic constant and these symbolic constants are called macro.
The general form of the directive is:
#define macro-name replacement-text
Simple Program of Preprocessor Directive
#include<iostream>
#define val 12
using namespace std;
int main()
{
cout << "Value is :" << val << endl;
}
There are different preprocessor directives that perform different tasks. You can categorize these Preprocessor Directives as follows:
- Inclusion Directives:
- #include: specifies the files to be included, especially header-files
- Macro Definition Directives:
- #define: define a macro substitution
- #undef: It is used for undefining a macro
- Conditional Compilation
Directives:
- #if: It tests a compile-time condition
- #elif
- #endif: It specifies the end of #if
- #ifdef: It is used to test for macro definition
- #ifndef: It tests whether a macro is not defined
- #else: It provides an alternative option when #if fails
- Other Directives:
- #error
- #line: Supplies a line number for compiler messages
- #pragma: It specifies implementation-defined instructions to the compiler