Advanced C language (program environment and preprocessing)

content

Foreword:

First, the translation environment and operating environment of the program

1. The translation environment of the program

Link stage:

2. Execution environment (running environment)

2. Detailed preprocessing

1. Predefined symbols

2. #define definition identifier

3. #define define macros

#define Substitution Rules

# and ## two preprocessing tools

4. Macro parameters with side effects

5. Macro and function comparison 

6. #undef remove macro

 7. Command Line Definition

8. Conditional compilation 

9, the header file contains 

Nested file contains 


Foreword:

Have you ever thought that as long as you press Ctrl+F5 on the compiler of vs2019, a source program of test.c can be turned into an executable program of .exe. How is this generated by compiling? Let's learn the knowledge and key preprocessing together with everyone.

First, the translation environment and operating environment of the program

Important: An implementation of any ANSI C (standard C program), there are two different environments

The first is a translation environment, in which source code is translated into executable machine instructions.

The second is the execution environment, which is used to actually execute the code.

  

1. The translation environment of the program

The translation environment is the act of compiling and linking that we do.

1. Each .c file is independently processed by the compiler, and finally generates a thing called an object file (the object file under windows is called obj)

2. Each object file is bundled together by the linker to form a single and complete executable program.

3. The linker will also introduce any functions used by the program in the standard C function library, and it can search the programmer's personal program library and link the functions it needs into the program.

 Compilation is divided into three steps:

Pre-compilation (preprocessing): gcc test.c -E (instruction) will stop after preprocessing, and the output of gcc test.c -E > test.i is redirected to the file test.i. At this stage, 1: Complete the inclusion of header files, such as preprocessing of directives such as #include, 2: Replacement of symbols and macros defined by #define, 3: Comment deletion. These are all text operations.

Compile: gcc test .i -S generates the test.s file. In this stage, the C language code is converted into assembly code, 1: Syntax analysis, 2: Lexical analysis, 3: Semantic analysis, 4: Symbol summary. 

 Assembly: Assemble the .s file, gcc test .s - c (instruction) to generate the test.o file (.o file is called test.obj in windows), this stage converts the assembly code into machine instructions (binary instructions), generate symbol table

Link stage:

Link multiple object files and link libraries.

At this stage, 1: merging segment tables, 2: merging and relocation of symbol tables.

2. Execution environment (running environment)

Program execution process:

1. The program must be loaded into memory. In an environment with an operating system: Usually this is done by the operating system. In a stand-alone environment, the loading of programs must be arranged manually, possibly by placing executable code into read-only memory.

2. The execution of the program starts. Then call the main function.

3. Start executing the program code. At this point the program will use a runtime stack (stack) to store the function's local variables and return addresses. Programs can also use static memory. Variables stored in static memory retain their values ​​throughout the execution of the program.

4. Terminate the program. Terminates the main function normally; it may also terminate unexpectedly.

2. Detailed preprocessing

1. Predefined symbols

__FILE__ //source file to compile

__LINE__ //The current line number of the file

__DATE__ //The date the file was compiled

__TIME__ //The time the file was compiled

__STDC__ //If the compiler follows ANSI C, its value is 1, otherwise undefined

Example: write a log file

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int main()
{
	int i = 0;
	FILE* pf = fopen("log.txt", "a+");
	if (NULL == pf)
	{
		perror("fopen\n");
		return 1;
	}
	for (i = 0; i < 10; i++)
	{
		fprintf(pf, "%s %d %s %s %d\n", __FILE__, __LINE__, __DATE__, __TIME__, i);
	}
	return 0;

}

document content:

2. #define definition identifier

Example:

3. #define define macros

The #define mechanism includes a provision that allows parameters to be substituted into text, an implementation commonly referred to as a macro or define macro.

The solution to this problem is to add a pair of parentheses around the macro definition expression.

#define DOUBLE( x)   ( ( x ) + ( x ) )

 So macro definitions used to evaluate numeric expressions should be parenthesized in this way to avoid unpredictable interactions between operators in arguments or adjacent operators when using the macro.

#define Substitution Rules

There are several steps involved when expanding #define to define symbols and macros in a program.

1. When calling the macro, the parameters are first checked to see if they contain any symbols defined by #define. If so, they are replaced first.

2. The replacement text is then inserted into the program in place of the original text. For macros, parameter names are replaced by their values.

3. Finally, scan the resulting file again to see if it contains any symbols defined by #define. If so, repeat the above process.

Notice:

1. Other #define-defined symbols can appear in macro parameters and #define definitions. But for macros, recursion cannot occur. 2. When the preprocessor searches for symbols defined by #define, the contents of string constants are not searched.

# and ## two preprocessing tools

# can insert parameters into the string 

 

## can combine the symbols on both sides of it into one symbol. It allows macro definitions to create identifiers from separate pieces of text. 

 

4. Macro parameters with side effects

 

 

When a macro parameter appears more than once in the macro definition, if the parameter has side effects, then you may be dangerous when using the macro, leading to unpredictable consequences. A side effect is a permanent effect that occurs when an expression is evaluated.

E.g:

x+1;//Without side effects

x++;//with side effects

 

5. Macro and function comparison 

Macros are usually used to perform simple operations. For example, find the larger of two numbers.

#define MAX(a, b) ((a)>(b)?(a):(b))

 So why not use functions to accomplish this task?

There are two reasons:

1. The code for calling and returning from the function may take more time than actually doing this small computational work. So macros outperform functions in terms of program size and speed.

2. More importantly, the parameters of the function must be declared as a specific type. So functions can only be used on expressions of the appropriate type. Conversely, how can this macro be applied to integers, long integers, floating point types, etc. that can be used for > to compare types.

Macros are type-independent.

Disadvantages of macros:

Of course, macros also have disadvantages compared to functions:

1. Every time a macro is used, a copy of the macro definition code will be inserted into the program. Unless the macro is relatively short, it can significantly increase the length of the program.

2. Macros cannot be debugged 

3. Macros are not rigorous enough because they are type-independent.

4. Macros may cause problems with operator precedence, making procedures prone to errors.

6. #undef remove macro

 This directive is used to remove a macro definition.

#undef NAME //If an existing name needs to be redefined, its old name needs to be removed first. 

code show as below:

 

 7. Command Line Definition

Many C compilers provide the ability to define symbols on the command line. Used to start the compilation process. For example, this feature is useful when we want to compile different versions of a program based on the same source file. (Assuming that a program declares an array of a certain length, if the machine memory is limited, we need a small array, but another machine memory is uppercase, and we need an array that can be uppercase.)

 

 

code show as below:

Under linux, through gcc test.c -DM=100, the following code can run normally

  

8. Conditional compilation 

Compile if the conditions are met, and do not participate in the compilation if the conditions are not met.

code show as below:

 Does not print the result. 

 

 

 

#if 1 is true, it will be compiled later, if it is 0, it will not be compiled. 

9, the header file contains 

'The header file includes two ways:

#include"add.h" The local file includes, the header file of the custom function uses ""

#include<add.h> The library file includes, the header file of the function provided in the C language library uses <>

 Search strategy: First search in the directory where the source file is located. If the header file is not found, the compiler will search for the header file in the standard location just like the library function header file. If it can't find it, it will prompt a compilation error

Find the header file and go directly to the standard path to find it. If it is not found, it will prompt a compilation error. Is it possible to say that library files can also be included in the form of ""? The answer is yes, yes. However, the search efficiency is lower in this way, and of course, it is not easy to distinguish whether it is a library file or a local file. 

Nested file contains 

At the beginning of each header file write:

#ifndef __TEST_H__

#define __TEST_H__

//content of header file

#endif   //__TEST_H__

or:

#pragma once

This avoids duplication of header files. 

Summarize:

The subject is too tired, so I won't write the summary. It's past midnight and I'm going to bed. I really feel like I can't stand it anymore. I have to get up early tomorrow. I hope everyone can stay true to their original aspirations, keep learning, and work hard together! ! ! ! !

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324085084&siteId=291194637