-
“#“和”##”
首先,C语言中的"#“和”##"是一种运算符,通常在宏定义中使用。 -
“#”
举一个列子:`
#define JKL(X) printf("this is # demo,input 2*X is %d.\n",((X)*2))
假设使用宏
JKL(5)
this is # demo,input 2*X is 10.
注意:这里双引号中的字符串包括X视为普通文本,不可替代。
#include <stdio.h>
#define JKL(X) printf("this is # demo,input 2*"#X" is %d.\n",((X)*2))
int main(void)
{
int y = 5;
JKL(y);
JKL(2+4);
JKL(2 +4);
return 0;
}
输出结果:
this is # demo,input 2*y is 10.
this is # demo,input 2*2+4 is 12.
this is # demo,input 2*2 +4 is 12.
这里中的#X本质为形参名,分别被替代为y,2+4,2 +4。一般称作为字符串化,同时还可以用来拼接字符串。
- “##”
2020/11/03,再次看到“#” 和“##”的用法,这次感觉理解更深刻。一句话,“#”用于字符串化,“##”用于拼接。举例:
#include <stdio.h>
#include <string.h>
#define str(x) #x
#define agltnt(x) s_##x##_day
int s_valentine_day = 214;
void main()
{
printf("%d",agltnt(valentine));
printf("%s"," happy");
printf(", I %s you\n",str(love));
return ;
}
运行一下,,,
这大概就是程序员的表达方式了吧