The preprocessor are the directives, which gives instruction to the compiler to do something before execution.
C++ supports various preprocessors like #include, #define, #if, #else, #line, etc.
The #define Preprocessor
This directive creates the symbolic constants.
#include <iostream>
using namespace std;
#define a 40;
int main ()
{
cout << "Value of a :" << a << endl;
return 0;
}
Macros:
You can use #define to define a macro used in the entire program.
#include <iostream>
using namespace std;
#define MIN(a,b) (((a)<(b)) ? a : b)
int main ()
{
int i, j;
i = 100;
j = 30;
cout <<"The minimum is " << MIN(i, j) << endl;
return 0;
}
Conditional Directives:
These directives are used to check a certain condition you want.
#ifndef NULL
#define NULL 0
#endif
#include <iostream>
using namespace std;
#define DEBUG
#define MIN(a,b) (((a)<(b)) ? a : b)
int main ()
{
int i, j;
i = 100;
j = 30;
#ifdef DEBUG
cerr <<"Trace: Inside main function" << endl;
#endif
#if 0
/* This is commented part */
cout << MKSTR(HELLO C++) << endl;
#endif
cout <<"The minimum is " << MIN(i, j) << endl;
#ifdef DEBUG
cerr <<"Trace: Coming out of main function" << endl;
#endif
return 0;
}
0 Comment(s)