In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the use of preprocessing in C language, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
The translation environment of programs
The environment in which the source file is converted into executable machine instructions is called the translation environment.
Converting from a source file (.c) to an executable file (.exe) requires two steps
Compilation is implemented through the compiler and links through the linker
Each source file is processed by the compiler to generate a corresponding target file, and then the linker links the target file with the link library to generate an executable program
Specific operations of compilation and linking
Compile
Compilation is divided into precompilation, compilation and assembly.
Precompile:
The inclusion of the 1.#include header file, replacing the reference to the header file with the specific declaration of the function.
two。 Delete comment
3.#define, preprocessing operation, replacing the define defined with the actual value
Compile:
Translate C code into assembly code
Syntax analysis, lexical analysis, semantic analysis, symbol summary (function names, global variables)
Compilation:
Convert assembly code to binary code to form a symbol table
Link
Links are divided into merging segment table and symbol table merging and symbol relocation.
Merge segment tables:
The target file has a certain format and is divided into several segments. The linker merges the data in the same segment of the target file.
Merging of symbol tables and relocation of symbols:
The linker will merge the symbol table into one table, and after the merge, when the symbols conflict, the invalid address will be relocated to a valid address, that is, after the merge.
After the link operation is completed, the executable program is generated.
Use of # define 1.#define defines identifiers, such as
When defining identifiers in define, it is best not to add semicolons ";" otherwise it will easily lead to syntax errors.
2.#define defines macros
The declaration of macros:
# define name (parament-list) stuff where parament-list is a symbol table separated by commas and may appear in stuff
Note: the left parenthesis of the parameter must be adjacent to name
Here's some code to see what you need to pay attention to when using macros.
The output is 11, not 36. What's the problem? we need to make clear the difference between a function and a macro. A function is a value that passes a parameter, while a macro is a replacement parameter. The essence of # define is replacement.
So X will be replaced with the expression 5'1, 5'1'5'1 and the result is obviously 11. If you want to improve, you can add more parentheses when using macros. Don't be stingy with parentheses.
Look at another example.
We found that the result was still not 100 as we thought, but 55. You should know that not only the parameter is replaced, but the whole formula is also replaced. Then DOUBLE (5) will be replaced with (5) + (5), then 10 * (5) + (5) will result in 55, and if you want to improve it, it will still be parenthesized.
So remember, don't be stingy with parentheses when using macros.
The 3.#define implementation inserts parameters into a string
Implementing the following # turns X into the string "X"
Output result:
Hello aworld
Thank you for reading this article carefully. I hope the article "what is the use of 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.