//test.c #include <stdio.h> #include <stdlib.h> //字符串化运算符 #define EXPAND(name) ({ printf("%s ", #name); }) //二元运算符 ## 将左和右操作数结合成一个记号 #define test(name, index) ( { int i, len = sizeof(name ## index) / sizeof(int); for (i = 0; i < len; i++) { printf("%d ", name ## index[i]); }}) int main(int argc, char *argv[]) { EXPAND(Test); EXPAND(""TEST"); EXPAND(__DATE__); EXPAND(__TIME__); EXPAND(__FILE__); EXPAND(__LINE__); EXPAND(__func__); int x1[] = { 1, 2, 3 }; int x2[] = { 11, 22, 33, 44, 55}; test(x, 1); test(x, 2); return 0; }
运行效果如下:
1 $ ./defind_test 2 Test 3 ""TEST" 4 __DATE__ 5 __TIME__ 6 __FILE__ 7 __LINE__ 8 __func__ 9 1 10 2 11 3 12 11 13 22 14 33 15 44 16 55