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

/*
  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

macros with multi line

/*
  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

© 2019, regularcodes.com, All rights reserved