当前位置:网站首页>The C programming language -- (2nd) -- Notes -- 4.11.2
The C programming language -- (2nd) -- Notes -- 4.11.2
2022-07-27 11:31:00 【YovaVan】
4.11.2 Macro replace (macro substitution)
The form of macro definition :
#define name replace text
This is the simplest macro replacement —— All subsequent occurrences name The marks will be replaced with replace text .#define Directive name [ Naming method ] It is named in the same way as the variable name ; replace text Is Ren ( The programmer ) I mean . Usually , The replacement text is define All the rest of the end of the instruction line , But a longer one Macro definitions can be defined through Add a backslash at the end of the line to be continued \ and Divided into several lines .#define The scope of the name defined by the instruction is from its Definition point Start , To Compiled source file ( The source file of the instruction ?) At the end of end . The previous... Can also be used in macro definitions [ Macro defined ] The macro definition of . Substitution is only for tokens , It has no effect on strings locked in quotation marks . Such as , if YES It's through #define Instruction defined name , It's in printf("YES") or YESMAN The replacement... Will not be performed .
[ whatever name Can be defined as any replace text ] The replacement text can be arbitrary , Such as :
#define forever for (;;) /* Infinite loop.*/by Infinite loop for(;;) Defined a new name forever.
Macro definitions can also take parameters , You can use different replacement text for different macro calls . give an example , Definition is named max The macro :
#define max(A, B) ((A) > (B) ? (A) : (B))Although it looks like a function call , Use macros max Insert replacement text directly into the code . Formal parameters ( Here is A or B) Each occurrence of will be replaced with the corresponding actual parameter . therefore , Instruction line
x = max(p+q, r+s);Will be replaced by :
x = ((p+q) > (r+s) ? (p+q) : (r+s)); As long as the processing of various parameters is consistent , The same macro can be applied to any data type ; need not , Define different types of... For different data types like functions max macro .
Check max The expansion of , Found some defects . The expression as a parameter is evaluated twice ; If the expression exists ( For example, it contains self increasing operator or input / Output ) Side effects are not good . for example :
max(i++, j++) /*WRONG*/The auto increment operation will be performed twice for each parameter . At the same time, we should also pay attention to the proper use of parentheses to ensure the correctness of the calculation sequence . Consider the following macro definitions :
#define square(x) x * x /*WRONG*/When used square(z+1) What happens when calling this macro definition ?
Even so , Macros are very valuable .<stdio.h> There is a very practical example in the header file :getchar And putchar In practice, functions are often defined as macros to avoid the running time overhead of calling functions when processing characters .<ctype.h> Functions defined in header files are often implemented through macros .
Names can be passed by #undef Instruction to cancel macro definition , This ensures that subsequent calls are function calls , Instead of macro calls :
#undef getchar
int getchar(void) {...}Formal parameters cannot be replaced with quoted strings . but , If the parameter name in the replacement text is with # As a prefix , Then the result will be expanded to “ Replace the... Of this parameter with the actual parameter ” Quoted string . for example , It can be combined with “ String concatenation operations ” Combine to write a debug print macro :
#define dprint(expr) printf(#expr " = %g\n", expr)Using statements
dprintf (x/y)When calling this macro , The macro will be extended to :
printf("x/y" " = &g\n", x/y); /* why changed '%' to '&' */The strings are connected , The effect of this macro call is equivalent to :
printf("x/y = &g\n", x/y);In the actual parameters , Every " Will be replaced by \" , Every \ Will be replaced by \\ , therefore Replaced string Is a legal string constant .
Preprocessor operators ## It provides Connect actual parameters Methods . If you replace a parameter in the text with ## adjacent , Then this parameter is replaced by the actual parameter ,## The white space before and after it will be deleted , And the replacement result will be rescanned . for example : Defined macro paste Connect its two parameters :
#define paste(front back) front ## backtherefore , paste(name, l) The token will be established namel.
## The rules used in the nesting of are more difficult ; See Appendix for details A.
practice 4-14 Defining macro swap(t, x, y) In exchange for two t Parameters of type .( The block structure will help .)
边栏推荐
- Today's code farmer girl summarized her notes about NPM package management and URL module
- Find the combination number acwing 887. find the combination number III
- Analysis of distributed database and cache double write consistency scheme (Reprint)
- Moveit2 - 5. Scenario Planning
- 求组合数 AcWing 886. 求组合数 II
- Knapsack model acwing 1024. Packing problem
- 力扣——10. 正则表达式匹配
- Kepserver configuration
- 博弈论 AcWing 893. 集合-Nim游戏
- 349 sum of intersection of two arrays and 01
猜你喜欢

博弈论 AcWing 891. Nim游戏

求组合数 AcWing 886. 求组合数 II

容斥原理 AcWing 890. 能被整除的数

数字三角形模型 AcWing 1018. 最低通行费

数字三角形模型 AcWing 1027. 方格取数

状态压缩DP AcWing 91. 最短Hamilton路径

WGet warning: unable to verify

Solutions to errors in tensorflow operation

【着色器实现Shake随机摇动效果_Shader效果第十篇】

The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence
随机推荐
FAQs of "relay chain" and "dot" in Poka ecosystem
第13章 IO流
(8) Shell function
What is private traffic?
XXX packages are looking for funding run 'NPM fund' for details solutions
Knapsack problem acwing 9. grouping knapsack problem
C programming language (2nd Edition) -- Reading Notes -- 1.5
Miscellaneous records of Finance
Digital triangle model acwing 1018. Minimum toll
Luogu p3052 [usaco12mar]cows in a skyscraper G
(9) Shell I / O redirection
Gaussian elimination acwing 883. solving linear equations with Gaussian elimination
Longest ascending subsequence model acwing 482. Chorus formation
Knapsack model acwing 1024. Packing problem
2022 Niuke multi school (3) j.journey
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
Description and feelings
Cancer DDD
Quantitative industry knowledge summary
洛谷P1441 砝码称重