Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Program Environment and preprocessing in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of program environment and preprocessing in C language, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.

The implementation of C language code includes two environments.

1. Translation environment, the source code into executable machine instructions 2. Execute environment, execute code 1. Translation environment

Includes two procedures, compilation and linking

Each source file in the program is converted into an object file (obj) through the compiler.

These target files are bundled together through the linker.

The linker also links the functions in the standard library as well as the programmer's personal library to the program.

Form an executable generation

Compilation is divided into three phases: precompilation (preprocessing)-> compilation-> assembly.

Pretreatment stage

1. Complete the reference to the header file and replace the macros and symbols defined by 2.#define 3. Deletion of comments

Compilation phase: convert the code to assembly code

1. Grammatical analysis II. Lexical analysis 3. Semantic analysis 4. Symbol summary

Assembler phase: generate a symbol table and convert assembly instructions into binary machine instructions

Link phase: link multiple target files to the link library

1. Table 2 of the merged section. Redirection and relocation of symbol tables 2. Running environment 1. In an environment with an operating system, programs are loaded into memory by the operating system. In a separate environment, this is done manually. two。 The program starts with the main function and executes 3. Program call stack 4. Termination procedure 3. Preprocessing the symbols defined by 3.1#define in detail

Symbol names are generally capitalized, do not be followed by

# define MAX 100

Macros defined by 3.2#define

# define SQUARE (x) ((x) * (x))

No spaces can be added between the names of macros and parameters; add as many parentheses as possible to avoid errors

The replacement rule of 3.3#define # define M 100#define MAX (Xpene Y) (X) > (Y)? (X): (y) int main () {int max = MAX (MMagne 50); / / the M here will be replaced by 100 printf ("% d", max);} 1. If there is a macro, first check whether the parameters of the macro have the symbol defined by # define, and if so, replace 2. 0. Replace the replaced text with the macro in the program by 3. Then check to see if there are defined symbols, and if so, replace them.

When replacing symbols defined by # define, the contents of string constants are not replaced.

Define-defined symbols can appear in the parameters of macros, but macros cannot implement recursion

3. Compare with # #

In the definition of a macro (# parameter), you can convert the parameter to the corresponding string.

# # can connect two symbols

4. Comparison between macros and functions

Advantages of macros

1. The parameter type of a macro does not need to declare 2. When the amount of computation is small, the operation time and code amount of the macro are much less than that of the function.

Shortcoming

1. Macro type is irrelevant and not rigorous enough. Macros cannot debug 3. Each time you use a macro, a copy of the code defined by the macro is inserted into the program. Unless macros are short, it is possible to significantly increase the length of the program. 4. Macros may bring the problem of operational priority 5.#undef

Used to remove macro definition

6. Conditional compilation

# if and # endif are grouped together. If the expression after # if is true, execute the code until # endif

But no variables can be written after # if

Conditional compilation of multiple branches # if,#elif,#elif,#else,#endif

Determine whether it is defined or not

# if defined (symbol)

# ifdef symbol

# if! defined (symbol)

# ifndef symbol

7. The file contains

If you use double quotation marks after # include, the compiler will look for it in the local directory first, and then go to the library function if you can't find it. Use the compiler to find it directly in the library function

To prevent the header file from being included repeatedly, use # pragma once and write it on the first line of the header file

Or use

# ifndef NAME

# define NAME

/ / contents of the header file

# endif

Thank you for reading this article carefully. I hope the article "sample Analysis of Program Environment and preprocessing in C language" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report