CPP Preprocessor
CPP Preprocessor
The preprocessors are the directives, which give instruction 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:
When this line appears in a file, all subsequent occurrences of macro in that file will be replaced by
replacement-text before the program is compiled. For example:
#include <iostream>
using namespace std;
#define PI 3.14159
int main ()
{
return 0;
}
Now, let us do the preprocessing of this code to see the result, assume we have source code file, so
let us compile it with -E option and redirect the result to test.p. Now, if you will check test.p, it will
have lots of information and at the bottom, you will fine the value replaced as follows:
...
int main ()
{
return 0;
}
Function-Like Macros:
You can use #define to define a macro which will take argument as follows:
#include <iostream>
using namespace std;
return 0;
}
If we compile and run above code, this would produce the following result:
The minimum is 30
Conditional Compilation:
There are several directives, which can use to compile selectively 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 and can debugging turn on or off using a
single macro as follows:
#ifdef DEBUG
cerr <<"Variable x = " << x << endl;
#endif
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
#include <iostream>
using namespace std;
#define DEBUG
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
If we compile and run above code, this would produce the following result:
#include <iostream>
using namespace std;
#define MKSTR( x ) #x
int main ()
{
cout << MKSTR(HELLO C++) << endl;
return 0;
}
If we compile and run above code, this would produce the following result:
HELLO C++
Let us see how it worked. It is simple to understand that the C++ preprocessor turns the line:
#define CONCAT( x, y ) x ## y
When CONCAT appears in the program, its arguments are concatenated and used to replace the
macro. For example, CONCATHELLO, C + + is replaced by "HELLO C++" in the program as follows.
#include <iostream>
using namespace std;
#define concat(a, b) a ## b
int main()
{
int xy = 100;
If we compile and run above code, this would produce the following result:
100
Let us see how it worked. It is simple to understand that the C++ preprocessor transforms:
Macro Description
__LINE__ This contain the current line number of the program when it is
being compiled.
__FILE__ This contain the current file name of the program when it is being
compiled.
__DATE__ This contains a string of the form month/day/year that is the date
of the translation of the source file into object code.
#include <iostream>
using namespace std;
int main ()
{
cout << "Value of __LINE__ : " << __LINE__ << endl;
cout << "Value of __FILE__ : " << __FILE__ << endl;
cout << "Value of __DATE__ : " << __DATE__ << endl;
cout << "Value of __TIME__ : " << __TIME__ << endl;
return 0;
}
If we compile and run above code, this would produce the following result:
Value of __LINE__ : 6
Value of __FILE__ : test.cpp
Value of __DATE__ : Feb 28 2011
Value of __TIME__ : 18:52:48
Loading [MathJax]/jax/output/HTML-CSS/jax.js