C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

Time:2021-6-14

As the name suggests, a preprocessor is a program that processes our source code before compilation. There are many steps involved between programming and executing in C / C + +. Let’s take a look at these steps before we actually start learning about the preprocessor.

C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

 

You can see the intermediate steps in the figure above. The source code written by the programmer is stored in the file program. C. The file is then processed by the preprocessor and generated into an extended source file called the program. The extension file is compiled by the compiler and generates an object code file named program. Obj. Finally, the linker links the object code file to the object code of the library function to generate the executable file program.exe.

Preprocessors provide preprocessor instructions that tell the compiler to preprocess the source code before compilation. All of these preprocessor instructions begin with a “((hash) symbol.”) The symbol means that any statement beginning with will enter the preprocessor and the preprocessor will execute the statement. Some examples of preprocessor instructions are: include, define, ifndef, etc. remember, the symbol only provides the path it will go to the preprocessor, and the command, for example, includes processing by the preprocessor program. For example, include will include additional code in the program. We can place these preprocessor instructions anywhere in the program.

There are four main types of preprocessor instructions

macro

File contains

Conditional compilation

Other instructions

Now let’s look at each of these instructions in detail.

macroMacro is a piece of code in a program with a certain name. Whenever the compiler encounters this name, it replaces it with the actual code fragment. “# The define instruction defines a macro. Now let’s use the program to understand the macro definition  

C++:

C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

 

C language:

C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

 

In the above program, when the compiler executes limit, it is replaced by 5. Limit in a macro definition is called a macro template, while 5 is a macro extension.

be careful: there is no semicolon (‘;’) at the end of the macro definition. Macro definitions do not need to end with semicolons.

Macro with parameters: we can also pass parameters to macros. A macro defined with parameters acts like a function. Let’s understand this through a program:  

C++:

C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

 

C language:

C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

 

From the above program, we can see that whenever the compiler finds area (L, b) in the program, it will replace it with statement (L * b). Moreover, the values passed to the macro template area (L, b) are replaced in the statement (L * b). Therefore, area (10,5) is equal to 10 * 5.

File contains: this type of preprocessor instruction tells the compiler to include files in the source code program. Users can include two types of files in the program:  

Header file or standard file: these files contain definitions of predefined functions, such as printf (), scanf (), etc. You must include these files to use these functions. Different functions are declared in different header files. For example, the standard I / O function is in the “iostream” file, while the function to perform string operations is in the “string” file.  

grammar: 

#include< file_name >

Where file_ Name is the name of the file to include Brackets tell the compiler to look for files in the standard directory.

User defined files: when a program becomes large, it’s a good practice to break it up into smaller files and include it when needed. These types of files are user-defined. These documents can include:  

#include”filename”

Conditional compilation: conditional compilation instructions are types of instructions that help to compile specific parts of a program according to certain conditions or skip the compilation of specific parts of a program. This can be done with two preprocessing commands“  ifdef ” And“  endif ” To do it.

grammar: 

#ifdef macro_name    

Statement 1;

Statement 2;

statement3; 

…..

statementN;

#endif

If the name is defined as“   macroname  ” If it is not defined, the compiler will skip the statement block directly.

Other instructions: in addition to the above instructions, there are two other instructions that are not commonly used. These are:  

#Undef instructionThe: undo instruction is used to undefine an existing macro. The working mode of the instruction is as follows:  

#undef LIMIT

Using this statement will undefine the existing macro limit. After this statement, each “# ifdef limit” statement evaluates to false.

#Pragma instruction: this instruction is a special-purpose instruction used to turn on or off certain functions. These instructions are compiler specific, that is, they vary from compiler to compiler. Some # pragma instructions are discussed below:  

#pragma startupand#pragma exit: these instructions help us specify the functions that need to be run before the program starts (before the control is passed to main()) and before the program exits (before the control returns from main()).  

be careful:The following procedure does not apply to GCC compilers.  

Look at the following program:

C++:

C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

 

C language:

C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

 

When running on gcc compiler, the above code will produce the following output:

Inside main()

This happens because GCC does not support # pragma to start or exit. However, you can use the following code for similar output on the gcc compiler.

C language:

C / C + + Programming Notes: C / C + + preprocessor, take you to learn C + + program better

 

#Pragma warn instruction:This directive is used to hide warning messages displayed during compilation.  

We can hide the warning as follows:

#pragma warn -rvl: this directive hides warnings that should be raised when the function that should return a value does not return a value.

#pragma warn -par: this directive hides warnings raised when a function does not use the arguments passed to it.

#pragma warn -rch: this directive hides warnings when code cannot be accessed. For example, any code written after the return statement of a function is not accessible.

Hope to help you~

In addition, if you want to better improve your programming ability, learn C language c + + programming! Overtaking on the curve, one step faster! I may be able to help you here~

C language c + + programming learning circle,QQ group 464501141Click to enterWeChat official account: C language programming learning base

Share (source code, project video, project notes, basic introductory course)

Welcome to change and learn programming partners, use more information to learn and grow faster than their own thinking Oh!

Programming learning book sharing:

Programming learning video sharing: