C/C++学习笔记
# 操作符
字符串化操作符,将传入的参数名转换成带引号的字符串:
1 2 3 4 5 6 7 8 9 10 11 12 13
| #define TOSTRING(str) #str
char s[] = TOSTRING(hello); printf("%s\n", s);
char s1[] = TOSTRING( hello ); printf("%s, %d\n", s1, sizeof(s1));
char s2[] = TOSTRING( hello world); printf("%s, %d\n", s2, sizeof(s2));
|
## 操作符
符号传递操作符,将多个参数合并成一个新的参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #define NumberOne 1 #define NumberTwo 2 #define GETNUMBER(num) Number##num
int n;
n = GETNUMBER(One); printf("%d\n", n);
#define GETNUMBER(num) Number ## num n = GETNUMBER(Two); printf("%d\n", n);
n = GETNUMBER(Three);
#define SSTRCAT(a, b) strcat(a##_p, #b);
char str1_p[20]; memset(str1_p, 0, 20); char str2_p[20]; memset(str2_p, 0, 20); strcat(str1_p, "111"); strcat(str2_p, "222");
SSTRCAT(str1, str2);
SSTRCAT(SSTRCAT(str1, str2), str2);
|
参考
cppreference
# Preprocessor Macro Operator
# and ## Operators in C