Thursday 29 November 2018

preprocessor directive in c++

Preprocessor directives (#include, #define)


A preprocessor is a collection of special statements which are executed before the compilation process.

Almost every C++ program contains a preprocessor directive.The #include 
preprocessor directives is commonly used to insert header file with extension ‘.h’.
These header files are already stored in computer in include directory. In the above program, two #include directives have been used,   #include<iostream.h>  and  
#include<conio.h>
#include<iostream.h> is used for the C++ object and #include<conio.h> for the built-in function getch().

In C++, the other commonly used preprocessor directive is #define which is used
 to define symbolic constant.Its general format is:

#define identifier value

For example:
#define PI 3.14159
#define NEWLINE ‘\n’

This define two new constant: PI and NEWLINE. Once they are defined,they can be used
in the rest of the program as if they were any other regular constant, for example:

#include<iostream.h>
#include<conio.h>
#define PI 3.14159
#define NEWLINE ‘\n’

int main()
{
double r = 5.0; //radius
double circle;
circle = 2 * PI * r;
cout <<”Area of the Circle :”<<circle;
cout<<NEWLINE;
getch();
return 0;
}

Output of the program
Area of the Circle : 31.4159

0 comments: