Preprocessor in C ++

Preprocessors are directives, which provide instructions to the compiler to preprocess the information before starting the actual compilation.

All preprocessor directives start with #, and only white-space spaces can appear before a preprocessor directive on a line. Preprocessor directives are not commands in C ++, so they do not end with a semicolon.

You have seen a preprocessor directive as #include in all examples. This macro is used to include a Header file in the source file.

There are some preprocessing directives supported by C ++ such as #include, #define, #if, #else, #line , . Below, we will show important preprocessor directives in C ++:

# Define preprocessor in C ++

#Define preprocessor directive creates constant symbols. The constant symbol is a macro and the general pattern of this preprocessor directive in C ++ is:

 #define ten_cua_macro ten_thay_the 

When this line appears in a file, all macros that appear later in this file will be replaced by ten_thay_the before the program is compiled. For example:

 #include using namespace std ; #define PI 3.14159 int main () { cout << "Gia tri cua PI la: " << PI << endl ; return 0 ; } 

Suppose we have a source file, then compile it with the –E option and direct the result to test.p. Now, if you test the test.p, it will have a lot of information and at the bottom, you will refine the replaced value as follows:

 $gcc - E test . cpp > test . p . int main () { cout << "Gia tri cua PI la: " << 3.14159 << endl ; return 0 ; } 

Function-Like Macro in C ++

You can use the #define preprocessor directive in C ++ to define a macro that takes the following parameters:

 #include using namespace std ; #define MIN ( a , b ) ((( a )<( b )) ? a : b ) int main () { int i , j ; i = 35 ; j = 16 ; cout << "Gia tri nho nhat la " << MIN ( i , j ) << endl ; return 0 ; } 

Compiling and executing the above code will produce the following results:

 Gia tri nho nhat la 16 

Conditional compilation in C ++

There are a number of preprocessing directives that can be used to compile selections among parts of your source code. This process is called conditional compilation.

Preprocessing instructions are quite similar to the if selection structure. You consider the following code:

 #ifndef NULL #define NULL 0 #endif 

You can compile a program for debugging purposes and can disable or enable this debug using a macro in C ++, as follows:

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

Cerr command to be compiled in the program if the constant DEBUG symbol has been defined before the #ifdef DEBUG directive. You can use the #if 0 command to annotate part of the program, as follows:

 #if 0 khong duoc bien dich phan code nay #endif 

Try the following example:

 #include using namespace std ; #define DEBUG #define MIN ( a , b ) ((( a )<( b )) ? a : b ) int main () { int i , j ; i = 35 ; j = 16 ; #ifdef DEBUG cerr << "Trace: Ben trong ham main!" << endl ; #endif #if 0 /* Day la phan comment */ cout << QTM ( Lap trinh C ++) << endl ; #endif cout << "Gia tri nho nhat la " << MIN ( i , j ) << endl ; #ifdef DEBUG cerr << "Trace: Ben ngoai ham main!" << endl ; #endif return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Preprocessor in C ++ Picture 1

The operators # and ## in C ++

The # and ## preprocessor operators are available in C ++ and ANSI / ISO C. The # operator announces that the replacement text will be converted to a string surrounded by quotation marks.

Consider the following macro definition:

 #include using namespace std ; #define QTM ( x ) #x int main () { cout << QTM ( Lap trinh C ++) << endl ; return 0 ; } 

Compiling and running the above code will produce the following results:

 Lap trinh C ++ 

Now, we consider how it worked. It is simple to understand that the C ++ preprocessor converts the following line:

 cout << QTM ( Lap trinh C ++) << endl ; 

Line:

 cout << "Lap trinh C++" << endl ; 

The ## operator is used to join a sequence of two tokens. For example:

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

When CONCAT appears in the program, its parameters are concatenated and used to replace the macro. For example, CONCAT (HELLO, C ++) is replaced by "HELLO C ++" as in the following program:

 #include using namespace std ; #define concat ( a , b ) a ## b int main () { char * xy = "Hello C++" ; cout << concat ( x , y ); return 0 ; } 

Compiling and executing the above code will result:

 Hello C ++ 

We now consider how they worked. Simple is the following C ++ preprocessor:

 cout << concat ( x , y ); 

Line:

 cout << xy ; 

Money macro defined in C ++

C ++ provides a number of pre-defined macros as listed below:

Macro Description__LINE__ Contains the current line number of the program when it is being compiled __FILE__ Contains the current file name of the program when it is being compiled __DATE__ Contains a month / day / year string as the date the source code is compiled __TIME__ Contains one string hour: minute: second is the time the program is compiled

Here is an example for all the above macros in C ++:

 #include using namespace std ; int main () { cout << "Gia tri cua __LINE__ la: " << __LINE__ << endl ; cout << "Gia tri cua __FILE__ la: " << __FILE__ << endl ; cout << "Gia tri cua __DATE__ la: " << __DATE__ << endl ; cout << "Gia tri cua __TIME__ la: " << __TIME__ << endl ; return 0 ; } 

Compile and run the above C ++ program to see the results:

According to Tutorialspoint

Previous article: Template in C ++

Next lesson: Signal Processing (Signal Handling) in C ++

5 ★ | 1 Vote

May be interested

  • 10 tips to help you become a great Pokemon trainer10 tips to help you become a great Pokemon trainer
    in the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
  • How to use GoChat application in Pokémon GOHow to use GoChat application in Pokémon GO
    pokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
  • Pokémon systems when fighting in Pokémon GoPokémon systems when fighting in Pokémon Go
    each pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.
  • The terms you need to know when playing Pokémon GoThe terms you need to know when playing Pokémon Go
    pokémon go is the most prominent name in recent days. this game of capturing and training virtual animals has created a relatively new way of playing, as players have to constantly move to catch pokémon. during the process of joining pokémon go, you will encounter and use a lot of important terms. so what do they mean?