WEB DEVELOPER SITE
PYTHONPHPJAVA
 

C++ preprocessor


The preprocessor is a directive that instructs the compiler to perform preprocessing before it is actually compiled.

All preprocessor directives begin with a pound sign (#), and only space characters can appear before the preprocessor directive. Preprocessor directives are not C++ statements, so they do not end with a semicolon (;).

We have seen that all previous instances have #include directives. This macro is used to include the header file in the source file.

C++ also supports a lot of preprocessor directives, such as #include, #define, #if, #else, #line, etc. Let's take a look at these important directives.

#define preprocessing

#define Preprocessor directives are used to create symbolic constants. This symbolic constant is often referred to as a macro, and the general form of the directive is:

#define macro-< /span>name replacement-text 

When this line of code appears in a file, all subsequent macros in the file will be replaced with replacement-text before the program is compiled. For example:

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

Now, let's test this code and see the results of the preprocessing. Assuming the source code file already exists, compile with the -E option and redirect the result to test.p. Now, if you look at the test.p file, you'll see that it already contains a lot of information, and the value at the bottom of the file is changed as follows:

$ gcc -E test.cpp > test.p

...
int main ()
{
 
    cout << "Value of PI :" << 3.14159 << endl; 

    return 0;
}

Parameter macro

You can use #define to define a macro with parameters as follows:

#include <iostream> using namespace std; #define MIN(a,b) (a<b ? a : b) int main () { int i, j; i = 100; j = 30; cout <<"Smaller value:" << MIN(i, j) << endl; return 0; }

When the above code is compiled and executed, it produces the following results:

The smaller value is:30

Conditional compilation

There are several instructions that can be used to selectively compile part of the source code. This process is called conditional compilation.

The conditional preprocessor has a structure similar to the if selection structure. Look at the code for this preprocessor:

#ifdef NULL
    #define NULL 0
#endif

You can compile only when debugging, and the debug switch can be implemented using a macro like this:

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

If the symbolic constant DEBUG has been defined before the directive #ifdef DEBUG, the cerr statement in the program will be compiled. You can comment out part of the program with the #if 0 statement as follows:

#if 0
   Code not compiled
#endif

Let's try the following example:

Instance

#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 the comment section*/ 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; }

When the above code is compiled and executed, it produces the following results:

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

# and ## operators

The

# and ## preprocessor operators are available in both C++ and ANSI/ISO C. The # operator converts the replacement-text token to a string enclosed in quotation marks.

Please see the macro definition below:

Instance

#include <iostream> using namespace std; #define MKSTR( x ) #x int main () { cout << MKSTR(HELLO C++) << endl; return 0; }

When the above code is compiled and executed, it produces the following results:

HELLO C++

Let's see how it works. It's not hard to understand that the C++ preprocessor puts this line:

cout << MKSTR(HELLO C++) << endl;

Converted to:

cout << "HELLO C++" << endl;
The

## operator is used to connect two tokens. Here's an example:

#define CONCAT( x, y )  x ## y

When CONCAT appears in the program, its parameters are concatenated and used to replace macros. For example, CONCAT(HELLO, C++) in the program will be replaced with "HELLO C++" as shown in the example below.

Instance

#include <iostream> using namespace std; #define concat(a, b) a ## b int main() { int xy = 100; cout << concat(x, y); return 0; }

When the above code is compiled and executed, it produces the following results:

100

Let's see how it works. It's not hard to understand that the C++ preprocessor puts this line:

cout << concat(x, y);

Converted to:

cout << xy;

Predefined macros in C++

C++ provides some of the predefined macros shown in the following table:

Macro Description
__LINE__ This will include the current line number when the program is compiled.
__FILE__ This will include the current file name when the program is compiled.
__DATE__ This will contain a string of the form month/day/year, which represents the date the source file was converted to the target code.
__TIME__ This will contain a string of the form hour:minute:second, which indicates when the program was compiled.

Let's take a look at an example of these macros:

Instance

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

When the above code is compiled and executed, it produces the following results:

Value of __LINE__ : 6
Value of __FILE__ : test.cpp
Value of __DATE__ : Feb 28 2011
Value of __TIME__ : 18:52:48