当前位置:网站首页>C language macro definition (macro parameter creation string, pretreatment adhesive)
C language macro definition (macro parameter creation string, pretreatment adhesive)
2022-07-16 06:34:00 【Sometimes crazy options】
#define CNAME value perhaps #define CNAME expression, Common definitions of constants are as follows , But the macro definition is much more flexible than expected . It can define macro constants 、 Macro functions , You can also enter data types . However , Macro definitions can also directly convert other types of data into char* String of type , Used as pretreatment adhesive .
1. Basic knowledge of macro definition
#define It is expanded in the preprocessing stage , as everyone knows C The compilation process of the language requires 4 Stage pretreatment 、 compile 、 assembly 、 Links here are not explained one by one , obviously define It will be launched in the initial stage ,define No type and safety checks . also define Will not be allocated memory space .
2.C Predefined macros in language ( Abstract )
__LINE__: Represents the line number of the current source code
__FILE__: Indicates the name of the current source file
__DATE__: Indicates the date when the file was compiled
__TIME__: Indicates when the file was compiled
__STDC__: The procedure is required to be strictly followed ANSI C The identifier is assigned as 1.
__func__ || __FUNCTION__: Indicates the name of the currently compiled function .
#include<stdio.h>
void print(void){
printf("%d\n",__LINE__);
printf("%s\n",__FILE__);
printf("%s\n",__DATE__);
printf("%s\n",__TIME__);
printf("%s\n",__func__);
printf("%s\n",__FUNCTION__);
printf("%d\n",__STDC__);
}
int main(){
print();
return 0;
}
Output results :
4
temp.c
Dec 16 2021
19:44:30
1
3. Macro functions ( Macro with parameters )
#define MACRO_NAME(ARGLIST) CONTENT
//MACRO_NAME Macro function name
//ARGLIST parameter list , No type is required , At this point, the variable length (...)
//CONTENT Replacement statement
Here is a simple example for everyone to understand .
#include<stdio.h>
#define T(type,b) type c = b
#define MAX(a,b) a>b?a:b
int main(){
T(int,10);
printf("%d\n",c);
printf("%d\n",MAX(2,3));
return 0;
}
Output results :
10
3
4. Create a string with macro parameters :# operation
use # No. creates a string
define STR(arg) #arg // You can convert any form of input into a string
#include<stdio.h>
#define STR(arg) #arg
int main(){
int x=1;
printf(STR(1234));
printf(STR(Hello world!));
printf(STR(x));
return 0;
}
~Output results :
1234
Hello world!
x // No 1 Because he will x Directly as characters
5. Pre processor adhesive : ##
Adhesion of variables , Very useful !!! Go straight to the code .
#include<stdio.h>
#define CON(a) v__##a
#define PRINT(n) printf("v__"#n"=%d\n",CON(n))
int main(){
int CON(1) = 1;
int CON(2) = 2;
int CON(a) = 3;
printf("%d\n",v__1);
printf("%d\n",v__2);
printf("%d\n",CON(a));
printf("%d\n",v__a);
PRINT(1);
PRINT(2);
PRINT(a);
return 0;
}
Output results :
1
2
3
3
v__1=1
v__2=2
v__a=3
边栏推荐
猜你喜欢

RT_thread邮箱的使用
![[Verilog] sub module connection related problems (adder and Its Optimization)](/img/20/60ba8379e23be8f258c76869aa7a1a.png)
[Verilog] sub module connection related problems (adder and Its Optimization)

如何将电子签名透明化处理

【MATLAB】matlab第二课——绘图初步
![[paper notes] - gan-2014-nips](/img/2a/4955c2d3755b72eff1185bc0402621.png)
[paper notes] - gan-2014-nips

keil5软件报错 Error: L6406E: No space in execution regions with .ANY selector matching xxx

嵌入式软件开发 STM32F407 蜂鸣器 寄存器版

pyopencv基础操作指南

第十二届蓝桥杯嵌入式模拟题

基于RT_thread的分布式无线温度监控系统实战(一)
随机推荐
Target detection (1) -- data preprocessing and data set segmentation
STM32入门之GPIO详解
CPU及内存占用过高,如何修改RTSP轮巡检测参数以降低服务器消耗?
【MATLAB】matlab第二课——绘图初步
C语言宏定义(宏参数创建字符串、预处理粘合剂)
anaconda常用指令
【Multisim】使用NE5532P系列运放仿真时必须注意的问题
RT_thread 临界区保护
POJ 3728 The merchant (在线查询+倍增lca)
Dhcp-master Automated Deployment
POJ 3417 Network (lca+树上差分)
第四章 STM32+LD3320+SYN6288+DHT11实现语音获取温湿度数值(下)
VScode自动添加注释
Embedded software development stm32f407 buzzer register version
【STM32F1】驱动DHT11(CubeMX配置)(HAL库)
Go语言从入门到规范-6.8、Go生成和解析json及注意事项
STM32—TIM3输出PWM信号驱动MG996R舵机(按键控制)
【Verilog】【Vivado】计数器示例
[Verilog] sub module connection related problems (adder and Its Optimization)
openMV实现颜色追踪