Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Preprocessor

C++ Preprocessor

The preprocessor is a set of directives that instruct the compiler to perform preprocessing before actual compilation.

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

As we have seen, all the previous examples contained #include directives. This macro is used to include header files into the source file.

C++ also supports many preprocessor directives, such as #include, #define, #if, #else, #line, etc. Let us look at these important directives together.

#define Preprocessor

The #define preprocessor directive is used to create symbolic constants. This symbolic constant is usually called amacro, and the general form of the directive is:


#define macro-name replacement-text 

When this line of code appears in a file, all subsequent occurrences of the macro in that 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 us test this code and see the result of preprocessing. Assuming the source code file already exists, compile it next using the -E option and redirect the result to test.p. Now, if you look at the test.p file, you will see that it already contains a large amount of information, and the value at the bottom of the file has been changed to the following:


$ gcc -E test.cpp > test.p

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

    return 0;
}

Parameterized Macros

You can use #define to define a macro with parameters, as shown below:

#include

<
iostream
using
namespace
std

;

#define

MIN

(

a

,

b

)

(

a

<
b
?
a

:

b

)

int

main

(

)

{

int

i

,

j

;

i

=

100

;

j

=

30

;

cout

<<

"

The smaller value is:

"

<<

MIN

(

i

,

j

)

<<

endl

;

return

0

;

}

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


较小的值为:30

Conditional Compilation

Several directives can be used to selectively compile parts of the program source code. This process is called conditional compilation.

The structure of conditional preprocessing is very similar to an if selection structure. See the preprocessor code below:


#ifdef NULL
   #define NULL 0
#endif

You can compile only during debugging; the debug switch can be implemented using a macro, as shown below:


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

If the symbolic constant DEBUG has been defined before the directive #ifdef DEBUG, then the cerr statements in the program will be compiled. You can use the #if 0 statement to comment out a part of the program, as shown below:


#if 0
   不进行编译的代码
#endif

Let us try the example below:

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 the commented-out 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

;

}

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


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 a replacement-text token into a quoted string.

See the macro definition below:

Example

#include

<
iostream
using
namespace
std

;

#define

MKSTR

(

x

)

x

int

main

(

)

{

cout

<<

MKSTR

(

HELLO

C

++

)

<<

endl

;

return

0

;

}

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


HELLO C++

Let us see how it works. It is easy to understand that the C++ preprocessor transforms the following line:


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

into:


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

The ## operator is used to concatenate two tokens. Here is an example:


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

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

Example

#include

<
iostream
using
namespace
std

;

#define

concat

(

a

,

b

)

a

b

int

main

(

)

{

int

xy

=

100

;

cout

<<

concat

(

x

,

y

)

;

return

0

;

}

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


100

Let us see how it works. It is easy to understand that the C++ preprocessor transforms the following line:


cout << concat(x, y);

into:


cout << xy;

Predefined Macros in C++

C++ provides some predefined macros shown in the table below:

macro Description
LINE This includes the current line number at program compile time.
FILE This includes the current file name at program compile time.
DATE This includes a string of the form month/day/year, representing the date the source file was converted to object code.
TIME This includes a string of the form hour:minute:second, representing the time the program was compiled.

Let us look at an example of the above macros:

Example

#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 code above is compiled and executed, it produces the following result:


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