What is Preprocessor Directive? What is the Purpose of Preprocessor Directives?

 


Preprocessor Directive

       Preprocessor directives is an instruction given to the compiler before the execution of the actual program. A preprocessor directive is also known as a compiler directive. The preprocessor directives are proceeded by a program known as preprocessor. It is a part of the C++ compiler. It modifies the C source program before compilation.

       Preprocessor directives start with a hash(#) symbol and the keyword include or define. These directives are written at the start of a program. The semicolon is not used at the end of preprocessor directives. The preprocessor directive is used in C language to include the header file in the program.


Types of Preprocessor Directives

       Two types of preprocessor directives used in C language are as follows:

1. Include Preprocessor


       The include preprocessor directive enables a program to access a library. Each library contains different header files. The include preprocessor directive is used to include the header file in the program.

Syntax


       The syntax of using this directive is as follows:
       #include <standard header file>

Example


       #include <stdio.h>
       #include <math.h>

 

       The above statement tells the preprocessor to include the file stdio.h and mat.h in the source program before compiling it.


The include directive tells the compiler where to find meanings of the standard identifies such as print. The meanings are described in the standard header file.
The header file stdio.h contains information about standard input and out functions such as scanf and printf.
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 a semicolon. It can be used anywhere in the program.

Syntax


       The syntax of define directive is as follows:
       #define    identifier  value


#                     It indicates the start of the preprocessor directive.
define          It is used to define a constant
identifier   It is the name of the constant.
value            It represented the value associated with the identifier.


       The preprocessor directive replaces all occurrences of the identifier with the blue. The identifier is conventionally written in uppercase.

Example


       #define PI    3.141593

Post a Comment

0 Comments