Latest web development tutorials

C ++ preprocessor

Some preprocessor directives, directs the preprocessor to the compiler before the actual compilation you need to complete.

All preprocessor directives are (#) beginning with the pound sign, only a space character may appear before the preprocessor directives. C ++ preprocessor directive is not a statement, so they are not a semicolon; at the end ().

We have seen all the previous instances have#include directive.This macro is used header file be included in the source file.

C ++ also supports a number of preprocessing directives, such as # include, # define, # if, # else, # line, etc., let us look at these important instructions.

#define preprocessing

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

#define macro-name replacement-text 

When this line of code appear in a file, the file appears in the subsequent All macros will be replaced with replacement-text before the program is compiled. E.g:

#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, take a look at the results of pretreatment. Suppose the source file already exists, then use the -E option to compile and to redirect the results to test.p. Now, if you look test.p file, you will see that it already contains a lot of information, and the value was changed in the bottom of the file as follows:

$gcc -E test.cpp > test.p

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

    return 0;
}

Macro Functions

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

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

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

The minimum is 30

Conditional compilation

There are several commands that can be used selectively on the part of the program source code is compiled. This process is known as conditional compilation.

Structure and choose if structural conditions much like the preprocessor. Consider the following passage preprocessor code:

#ifndef NULL
   #define NULL 0
#endif

You can make only when compiled with debugging, debugging can be realized using a macro, as follows:

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

If before the instruction #ifdef DEBUG has been defined symbolic constant DEBUG, it will be on the programcerr statements compiled.You can use #if 0 comment out the statement part of the program, as follows:

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

Let's try the following examples:

#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
   /* 这是注释部分 */
   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

# ## And preprocessing operator in C ++ and ANSI / ISO C in it is available. # Operator token replacement-text will be converted to a string enclosed in quotation marks.

Consider the following macro definition:

#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. Understandably, C ++ preprocessor the following line:

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

Converted into:

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

## Operators are used to connect two tokens. Here is an example:

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

When CONCAT appears in the program, its parameters will be linked, and used to replace the macro. For example, the program CONCAT (HELLO, C ++) is replaced by "HELLO C ++", as shown in the following examples.

#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. Understandably, C ++ preprocessor the following line:

cout << concat(x, y);

Converted into:

cout << xy;

C ++ predefined macros

C ++ is provided in the table below some predefined macros:

描述
__LINE__ 这会在程序编译时包含当前行号。
__FILE__ 这会在程序编译时包含当前文件名。
__DATE__ 这会包含一个形式为 month/day/year 的字符串,它表示把源文件转换为目标代码的日期。
__TIME__ 这会包含一个形式为 hour:minute:second 的字符串,它表示程序被编译的时间。

Let us look at these macros examples:

#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