Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The preprocessor for C+


May 11, 2021 C++


Table of contents


The preprocessor for C+

Preprocessors are instructions that indicate the preprocessing that the compiler needs to complete before it actually compiles.

All preprocessor instructions begin with a hashtag, and only space characters can appear before the preprocessing instruction. P reprocessing instructions are not C++ statements, so they are not issued with a sign (; The end.

As we've seen, all previous instances have #include instructions. This macro is used to include the header file in the source file.

It also supports a number of preprocessing instructions, such as #include, #define, #if, #else, #line, and so on, so let's look at these important instructions.

#define pre-processing

#define preprocessing instructions are used to create symbolic constants. This symbol constant is often referred to as a macro, and the general form of an instruction is:

#define macro-name replacement-text 

When this line of code appears in a file, all subsequent macros in that file will be replaced with replace-text before the program compiles. 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. A ssuming that the source code file already exists, the next step is to compile using the -E option and redirect the results 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 to the following:

$gcc -E test.cpp > test.p

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

    return 0;
}

The function macro

You #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 <<"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 instructions that you can use to selectively compile some program source code. This process is called conditional compilation.

The structure of the conditional preprocessor is similar to the if selection structure. Take a look at the preprocessor code below:

#ifndef NULL
   #define NULL 0
#endif

You can compile only when debugging, and the debug switch can be implemented with a macro, as follows:

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

If the #ifdef DEBUG has been defined before the instruction is completed, the cerr statement in the program is compiled. You #if part of the program using the 0 statement, as follows:

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

Let's try the following 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
   /* 这是注释部分 */
   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

The operators of the . . . and .

The pre-processing operators are available in both C and ANSI/ISO C. The operator converts the replace-text token into a string that is quoted.

Take a look at the macro definition below:

#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 put the following 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 connected and used to replace the macro. For example, conCAT (HELLO, C) in a program is replaced with "HELLO C", as shown in the following 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 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 put the following line:

cout << concat(x, y); 

Converted to:

cout << xy; 

Predefined macros in C+

Some of the predefined macros shown in the following table are provided by C+

Macro describe
__LINE__ This contains the current line number when compiling.
__FILE__ This contains the current file name when compiling.
__DATE__ This will contain a string of Month / Day / Year, which indicates the date of converting the source file to the target code.
__TIME__ This will contain a string of Hour: minute: Second, which indicates the time that the program is compiled.

Let's look at examples of these macros above:

#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