Preprocessor directive is an instruction given to the compiler before the execution of actual program. It is also known as compiler directive. A preprocessor is a part of C++ compiler. The semi colon is not used at the end of a preprocessor directive.
The preprocessor directives start with hash symbol # and the keyword include or define. These directives are written at the start of the program. It is used to include header files in the program.
Types of Preprocessor directives
There are two types of preprocessor directives that are used in C++ program.
- Include Preprocessor:
This enables a program to access a library. Each library contains different header files. The include preprocessor directive is used to include header files in the program.
The syntax is as follows:
# include <standard header file >
# include <conio.h>
# include <math.h>
The above statement tells preprocessor to include the file conio.h and math.h in source program before compiling it.
The include directive tells the compiler where to find the meanings of standard identifiers such as getch(). The meanings are described in standard header files. The math.h contains information about common mathematical functions.
- Define preprocessor:
The define directive is used to define a constant. It starts with the symbol #. It is not terminated with semicolon. It can be used anywhere in the program.
The syntax of define directive is as follows:
#define identifier value
# indicates the start of the preprocessor directive.
define is used to define a constant.
identifier is the name of the constant.
value represents the value associated with the identifier.
The preprocessor directive replaces all occurrences of the identifier with the value. The identifier is conventionally written in uppercase.
#define PI 3.14