C++宏定义中的## 和#的区别
我们用一来实例来学习一下C++宏定义中的## 和#的区别,首先我们来定义一个宏:#define STRCPY(a, b) strcpy(a ## _p, #b)
1. 宏定义里面有个##表示把字符串联在一起。如:
#include <stdio.h>
#define CAT(x,y) x##y
int main()
{
printf(\"%s\", CAT(\"hello\", \" world\"));
return 0;
}
2.宏定义中的#表示将其变为字符串。如:
#include <stdio.h>
#include<string.h>
#define STRCPY(a, b) strcpy(a ##_p, #b)
int main()
{
char arrr_p[]=\"abcdefg\";
char *b = \"123456\";
STRCPY(arrr, b);
return 0;
}
结果为把b变成了字符串,可见#b的功能是将所有类型名都变成了字符串。 另,a、_p和##有没有空格不影响结果。
1. 宏定义里面有个##表示把字符串联在一起。如:
#include <stdio.h>
#define CAT(x,y) x##y
int main()
{
printf(\"%s\", CAT(\"hello\", \" world\"));
return 0;
}
2.宏定义中的#表示将其变为字符串。如:
#include <stdio.h>
#include<string.h>
#define STRCPY(a, b) strcpy(a ##_p, #b)
int main()
{
char arrr_p[]=\"abcdefg\";
char *b = \"123456\";
STRCPY(arrr, b);
return 0;
}
结果为把b变成了字符串,可见#b的功能是将所有类型名都变成了字符串。 另,a、_p和##有没有空格不影响结果。
顶(0)
踩(0)
上一篇:C++中对文件的读写的实现方法
下一篇:基础知识:C++的模板及模板特化
- 最新评论
