1 // #表示:对应变量字符串化
2 // ##表示:把宏参数名与宏定义代码序列中的标识符连接在一起,形成一个新的标识符
3 #include <stdio.h>
4 #define trace(x, format) printf(#x " = %" #format "\n", x)
5 #define trace2(i) trace(x##i, d)
6
7 int main()
8 {
9 int i=1;
10 char *s="three";
11 float x=2.0;
12
13 trace(i, d); // 相当于 printf("i = %d\n", x) ,输出结果:i = 1
14 trace(x, f); // 相当于 printf("x = %f\n", x) ,输出结果:x = 2.000000
15 trace(s, s); // 相当于 printf("s = %s\n", x) ,输出结果:s = three
16
17 int x1=1, x2=2, x3=3;
18 trace2(1); // 相当于 trace(x1, d),输出结果:x1 = 1
19 trace2(2); // 相当于 trace(x2, d),输出结果:x2 = 2
20 trace2(3); // 相当于 trace(x3, d),输出结果:x3 = 3
21
22 return 0;
23 }