C++ Preprocessor
Preprocessor is an directives which are used to defined micros statement and reliable to includes header file in our source code. that are starts with # (hash symbol) followed by directive names. This are executed before actual compilation process and provide reliable environments.
Preprocessor is an separate process they are executed just before a compilation process. this are scan every source code file in top to bottom. and if find micros statement then it will work on relative task.
#define Preprocessor
defined preprocessor is also known as micro expansion. because that are replace single statement and group of statement in symbolic constants. see the following examples.
Replace statements
#include<iostream>
using namespace std;
#define START cout<<" Learn C++";
int main(){
START; //execute micros
return 0;
}
Output
Learn C++
Macros with argument
Preprocessor are capable to create function with arguments. speciality of this functions there are no need to define data type of function parameter list. see an example.
/*
Macros with arguments
*/
#include<iostream>
using namespace std;
#define MAX(a,b) (a>b)?a:b //Macros arguments
int main(){
int max_int=MAX(21,41);
float max_float=MAX(100.20,50.34);
char max_char=MAX('A','a');
cout<<"max int :"<<max_int<<endl;
cout<<"max float :"<<max_float<<endl;
cout<<"max char :"<<max_char;
}
Output
max int :41
max float :100.2
max char :a
Note that in given program MAX is an macros function. that are accept two arguments. that are not initial defined that are which type of data value accept. this are capable to work on all inbuilt data type. But note that in this function are not define any return type. this are returning the largest value.
this returning value are store in result variable. assume that this value are not store by any variable. so we cannot access this return value by cout object. for example.
/*
Macros with arguments
*/
#include<iostream>
using namespace std;
#define MAX(a,b) (a>b)?a:b //Macros arguments
int main(){
//array store intger value
int array[]={1,6,32,75,13,56};
int index=1,result=0;
while(index<7){
//case 1 store value
result=MAX(array[index-1],array[index]);
cout<<"result :"<<result<<endl;
//case 2 not store returnning value and
//try to print that values
cout<<"try access :"<<MAX(array[index-1],array[index]);
cout<<endl;
index=index+2;
}
}
Output
result :6
try access :0
result :75
try access :0
result :56
try access :0
Note that in normal c++ program are generate both of case are same result. In this program ostream class object are not display valid result during the run time.
macros with multi line
if including are / (forward slash) on end of micros statement . That means considered next incoming line is also part of macros. so using of forward slash can be create multi line statement.
/*
Macros with multi line
*/
#include<iostream>
using namespace std;
#define DISPLAY(a) for(int b=1;b<=a;b++){ \
cout<<b<<endl; \
}
int main(){
DISPLAY(5);
}
Output
1
2
3
4
5
Macros with constant value
/*
Macros with constant value
*/
#include<iostream>
using namespace std;
#define PI 3.14159
int main(){
cout<<"PI :"<<PI;
}
Output
PI :3.14159
Conditional Compilation
Conditional preprocessor macros are used to check condition statements. there are as in follows.
#ifdef : This directives are used to check given macros are exist or not in our program. if exist then executes inside instruction.
#else : This is an default and optional micros. this are used in when not satisfied the condition of #ifdef micros.
#endif : This are used to terminate #ifdef condition
#ifndef : This an special micros. this are work when given micros statement are not exist.
/*
Example conditional micros
#ifdef,#else,#endif,#ifndef
*/
#include<iostream>
using namespace std;
#define DISPLAY(a) cout<<a;
int main(){
#ifdef DISPLAY //start of condition
//statements and code here
cout<<" DISPLAY Function exist in program."<<endl;
#endif //end of #ifdef
//if and else conditional pair
#ifdef PI //start of condition
//if condition is true then
cout<<" PI are exist in this program. "<<endl;
#else
cout<<" Opps PI not exist in this program. "<<endl;
#endif //end of #ifdef PI
//check not defined
#ifndef PI
#define PI 3.14159
cout<<" Define PI"<<endl;
#endif
cout<<" PI :"<<PI;
return 0;
}
Output
DISPLAY Function exist in program.
Opps PI not exist in this program.
Define PI
PI :3.14159.
Predefined Macros
C++ are provides few standard predefined macros. there are as follows.
1) __DATE__ : This will return current date of your system. Format of date is "MM : DD : YYYY".
2) __FILE__ : This is returning the name of source file. which are create .exe file in compilation time.
3) __TIME__ : Returning the current time of your system. Time Format is "HH:MM:SS".
4) __LINE__ : returning Line number. current location of macros.
5) __STDC__ : Compiler compile ASCII standards then it will returning 1.
/*
Example of Predefined Macros.
*/
#include<iostream>
using namespace std;
int main(){
cout<<"DATE : "<<__DATE__<<endl;
cout<<"FILE : "<<__FILE__<<endl;
cout<<"TIME : "<<__TIME__<<endl;
cout<<"LINE : "<<__LINE__<<endl;
cout<<"STDC : "<<__STDC__<<endl;
return 0;
}
Output
DATE : Jan 29 2019
FILE : example.cpp
TIME : 03:59:47
LINE : 12
STDC : 1
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment