# How a Preprocessor works in C/C++?

Here we will see how the preprocessors are working in C or C++. Let us see what are the preprocessors.

The preprocessors are the directives, which give instructions to the compiler to preprocess the information before actual compilation starts.

All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do not end in a semicolon (;).

You already have seen a #include directive in all the examples. This macro is used to include a header file into the source file.

There are number of preprocessor directives supported by C++ like #include, #define, #if, #else, #line, etc. Let us see important directives −

## The #define preprocessor

The #define preprocessor directive creates symbolic constants. The symbolic constant is called a macro and the general form of the directive is −

#define macro-name replacement-text

## Example

#include <iostream>
using namespace std;
#define PI 3.14159
int main () {
cout << "Value of PI :" << PI << endl;
return 0;
}

## Output

Value of PI :3.14159

## Conditional Compilations

There are several directives, which can be used to compile selective portions of your program's source code.This process is called conditional compilation.

The conditional preprocessor construct is much like the ‘if’ selection structure. Consider the following preprocessor code −

#ifndef NULL
#define NULL 0
#endif

You can compile a program for debugging purpose. You can also turn on or off the debugging using a single macro as follows −

#ifdef DEBUG
cerr <<"Variable x = " << x << endl;
#endif

This causes the cerr statement to be compiled in the program if the symbolic constant DEBUG has been defined before directive #ifdef DEBUG. You can use #if 0 statment to comment out a portion of the program as follows −

#if 0
code prevented from compiling
#endif

## Example

#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;
}

## Output

Trace: Inside main function
The minimum is 30
Trace: Coming out of main function