[basic c++]c中的宏重复展开

2018-07-09  本文已影响28人  Quasars

c中的宏重复展开

1   #include <cstdio>
2
3   #define H0 "a0"
4   #define H1 "b0"
5   #define H2 "c0"
6
7   #define GetH(x) H##x
8
9     #define STR(x) #x
10  #define STRHELPER(x) STR(x)
11  #define CONCAT(a,b) a##b
12
13
14  int test1() {
15      printf("%s\n", GetH(0));                    // compiler will repeat scaning for macro targets.
16      return 0;
17  }
18
19
20  int test2() {
21      printf("%s\n", STR(CONCAT(I,PAD)));         // directly call STR, output: CONCAT(I,PAD)
22      printf("%s\n", STRHELPER(CONCAT(I,PAD)));   // call the helper macro, output: IPAD
23      return 0;
24  }
25
26
27  int main() {
28      test2();
29      return 0;
30  }
上一篇 下一篇

猜你喜欢

热点阅读