当前位置:网站首页>Stringing of macro parameters and connection of macro parameters in C language
Stringing of macro parameters and connection of macro parameters in C language
2022-07-26 10:50:00 【aLLLiyyy】
c Macros in language have macros with or without parameters , There are also macros with parameters .
In fact, a macro is equivalent to a string template , Then the preprocessor is at the mark with macro , Expand the macro , Explain the white point , That is, string replacement . But this work happens before compilation , I didn't understand it before , Feeling c How big are macros in language , It's just for you c The code does some string replacement , After replacement , still c Language , Then it begins to compile into instructions such as assembly .
Now let's talk about how to define a macro with parameters , For example, the simplest macro definition
#define MAX(a,b) (a)>(b)?(a):(b)
such , When we meet in code MAX() Sign time , Precompiled processor ( Note that it's not the compiler ) Will automatically replace this template , Then expand and compare the size of two numbers ;
As shown below :
int getMax(int x,int y) {
return MAX(x, y);
}
On the surface, it seems to reference a macro , In fact, the string is replaced by the preprocessor , And that's what happened :
int getMax(int x,int y) {
return (x)>(y)?(x):(y);
}
See that? , Macros are just replacement strings , It can't be simpler , I just don't know why other languages don't have the concept of macros .
Understand the concept of macro , that . Let's do two operations on macro parameters , String macro parameters , Connection of macro parameters .
1. String of red parameters , as follows :
#define STR(a) #a
adopt # This character , You can set the parameters in the macro a Change to string , Examples are as follows
cout << STR(hello everyone!) << endl;
such , You can print out hello everyone! This string !
2. Simple connection of macro parameters .
#define JOIN(a,b) a##b
That's easy Of a,b The contents of the two variables do a string splicing operation , The following code will output 1234.
cout << JOIN(12,34) << endl;
Be careful , Just a simple string connection . There is no operation of addition, subtraction, multiplication and division .
边栏推荐
- 剑指Offer(五):用两个栈实现队列
- [leetcode daily question 2021/5/8]1723. The shortest time to complete all work
- LIst和Dictionary实例应用(※)
- Koin
- Oracle create index
- @NotBlank、@NotNull 、@NotEmpty 区别和使用
- SQL Server 之Sql语句创建数据库
- Tutorial of putty
- RT-Thread 学习笔记(三)---用SCons 构建编译环境
- WinPcap packet capturing function pcap_ Loop (), stop the problem
猜你喜欢

mysql20210906
![[leetcode daily question 2021/4/23]368. Maximum divisible subset](/img/0b/32ca862963c842a93f79eaac94fb98.png)
[leetcode daily question 2021/4/23]368. Maximum divisible subset

232.用栈实现队列

Bash shell学习笔记(四)

Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
![[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits](/img/de/62fca587cde95110c2a967ca93eea5.png)
[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits

微信公众号消息通知 “errcode“:40164,“errmsg“:“invalid ip
![[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]

242.有效的字母异位词

RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)
随机推荐
Codepoint 58880 not found in font, aborting. flutter build apk时报错
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
RT-Thread 学习笔记(六)--- 开启基于SPI Flash的elmfat文件系统(上)
shell 脚本 失败自动重复执行
RT-Thread 学习笔记(八)---开启基于SPI Flash的elmfat文件系统(下)
很多人都不清楚自己找的是Kanban软件还是看板软件
Kali view IP address
使用float实现左中右布局,中间内容自适应
Happens-Before原则深入解读
二叉树的遍历 递归+迭代
[dectectron2] follow the official demo
mysql20210906
微信公众号开发 获取openid时报错40029 invalid code 问题的解决
Sword finger offer (V): queue with two stacks
IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
RT thread learning notes (III) -- building a compilation environment with scons
Many people don't know whether they are looking for Kanban software or Kanban software
创建EOS账户 Action
Flutter TextField设置高度并且自动换行,圆角边框去除下划线
Koin