1. # 与##的区别

    1.1. #宏定义时转变参数为字符串


e.g.1:
#define STR(str)      #str
char *mystr = STR(test);
printf("%s",mystr);
result: "test"

e.g.2:
#define STR(str)      #str
char *myits =STR(1024);
printf("%s",myits);
result: "1024"

e.g.3:
#define STR(str)       #str
#define SIZE           1024
char *myint =STR(SIZE);
printf("%s",myint);
result: "SIZE" //此处输出式SIZE 因为SIZE是一个宏,不会再展开

e.g.4:
#define _STR(str)      #str
#define STR(str)       _STR(str)
#define SIZE           1024
char *myint =STR(SIZE);
printf("%s",myint);
result: "1024" //增加了中间层,可以正确转换
View Code

    1.2. ##用于宏定义时将两个内容进行拼接

e.g.1:
#define LINK2(a,b)      a##b
char *mystr = LINK2(o,k);
printf("%s",mystr);
result: "ok"

e.g.2:
#define LINK3(a,b,c)      a##b##c
char *mystr = LINK3(c,+,+);
printf("%s",mystr);
result: "c++"
View Code

2. gcc  的参数中添加宏

    -Dmacro             等价于代码里   #define macro
    -Dmacro=value  等价于代码里   #define macro value
    -Umacro            等价于代码里    #undef macro

参考文献:https://www.cnblogs.com/xuybin/p/3175438.html

参考文献:https://blog.csdn.net/u010058695/article/details/100519877

12-18 21:55