C语言中的宏

C/C++中有些特殊的宏定义,面试时候被问到,写个短文总结下。

宏定义中的#和##连字符

这两个字符在宏定义中代表连接和替换,

  • #紧跟字母表示对应字符的字符串化,将对应的字符转换成对应的字符串,比如#hello就是"hello"
  • ##表示将宏定义中的两个标识符连接在一起,组成一个新的标识符,类似胶水。它首先查看##两边的字符,是否有宏定义可以替换的字符串,替换之,之后将两个连接在一起。

下面举例说明。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#define trace(x, format) printf(#x " = %" #format "\n", x)
#define trace2(i) trace(x##i, d)

int main(int argc, _TCHAR* argv[])
{
    int i = 1;
    char *s = "three";
    float x = 2.0;

    trace(i, d);                // 相当于 printf("x = %d\n", x)
    trace(x, f);                // 相当于 printf("x = %f\n", x)
    trace(s, s);                // 相当于 printf("x = %s\n", x)

    int x1 = 1, x2 = 2, x3 = 3;
    trace2(1);                  // 相当于 trace(x1, d)
    trace2(2);                  // 相当于 trace(x2, d)
    trace2(3);                  // 相当于 trace(x3, d)

    return 0;
}

又比如

1
2
3
4
#define STACK_ADD_TASK_IRAM_POOL1(TASK_NAME, task_name, task_type){\
task_info_g[INDEX_##TASK_NAME].task_entry_func = \
    (osa_task_func_ptr) stack_##task_type##_task;\
}

上面的语句中,我们会将其中的TASK_NAMEtask_type替换掉,并与前后的标识符相连接生成新的标识符。

宏定义中的 do{} while(0)

经常在宏定义中会看到如下的语句

1
2
3
4
5
6
7
#define STACK_ADD_MULTI_TASK(multi_sys_max, TASK_NAME, task_name, task_type)  \
    do{                                                                       \
       if(multi_sys_max >= 1)                                                 \
       {                                                                      \
           STACK_ADD_TASK_1_CARD(TASK_NAME, task_name, task_type);            \
       }                                                                      \
    }while(0)

这个语句的特点是宏定义之后紧跟一个do{...}while(0)的结构,看起来颇为繁琐,那这样的定义有什么好处呢?

首先,C 中的宏定义,在预编译阶段就会将宏定义的结构替换掉,使用宏定义定义函数。在代码替换中肯定希望像使用定义的函数使用宏定义,比如上面的语句在代码中肯定是下面这样的

1
STACK_ADD_MULTI_TASK(multi_sys_max, TASK_NAME, task_name, task_type);

所以注意到宏定义的while(0)后面没有分号

然后,这个结构和if..else...的控制结构可以完美结合,对照上面的宏定义,一般能想到的宏定义的结果修改如下

1
2
3
4
5
6
7
#define STACK_ADD_MULTI_TASK(multi_sys_max, TASK_NAME, task_name, task_type)  \
    {                                                                         \
       if(multi_sys_max >= 1)                                                 \
       {                                                                      \
           STACK_ADD_TASK_1_CARD(TASK_NAME, task_name, task_type);            \
       }                                                                      \
    }

如果正常替换之前的语句,替换之后的结果就是

1
2
3
4
5
6
{                                                                         \
   if(multi_sys_max >= 1)                                                 \
   {                                                                      \
       STACK_ADD_TASK_1_CARD(TASK_NAME, task_name, task_type);            \
   }                                                                      \
};

注意最后的分号,这一行编译不通过,但是很明显如果换成do{}while(0)结构就不存在这个问题。

其次,如果使用宏定义多行语句,那么使用大括号的宏定义嵌套在if...else...的结构中会遇到问题,比如定义

1
#define BAR(X) f(x); g(x)

如果用在下面的程序中就会出现语法错误

1
2
3
4
5
6
if(true)
    BAR(x)
else
   {
       //do nothing
   }

但是使用do{...}while(0)结构就不会有这样的问题。

__DATE__,__TIME__,__FILE__,__LINE__特殊宏定义

  • _LINE_:在源代码中插入当前源代码行号;
  • _FILE_:在源文件中插入当前源文件名;
  • _DATE_:在源文件中插入当前的编译日期
  • _TIME_:在源文件中插入当前编译时间;
  • _STDC_:当要求程序严格遵循ANSI C标准时该标识被赋值为1;
  • __cplusplus:当编写C++程序时该标识符被定义

具体的使用方式参考如下的代码

 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
#include <stdio.h>

int main(void) {
    int answer;
    short x = 1;
    long y = 2;
    float u = 3.0;
    double v = 4.4;
    long double w = 5.54;
    char c = 'p';;

    // __DATE__, __TIME__, __FILE__, __LINE__ 为预定义宏
    printf("Date : %s\n", __DATE__);
    printf("Time : %s\n", __TIME__);
    printf("File : %s\n", __FILE__);
    printf("Line : %d\n", __LINE__);
    printf("Enter 1 or 0 : ");
    scanf("%d", &answer);

    // 这是一个条件表达式
    printf("%s\n", answer?"You sayd YES":"You said NO");

    // 各种数据类型的长度
    printf("The size of int %d\n", sizeof(answer));
    printf("The size of short %d\n", sizeof(x));
    printf("The size of long %d\n", sizeof(y));
    printf("The size of float %d\n", sizeof(u));
    printf("The size of double %d\n", sizeof(v));
    printf("The size of long double %d\n", sizeof(w));
    printf("The size of char %d\n", sizeof(c));
}

__VA_ARGS__宏定义

当前主干代码的一个典型的用法如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#define DEFINE_KERNEL(gobalFunc, ...) \
 extern "C" __global__ __aicore__ void gobalFunc(__gm__ half* tableAddr, TABLE_SIZE_INT64 tableSize) \
 { \
 __VA_ARGS__(tableAddr, tableSize); \
 } \
\
 DLL_PUBLIC void run_##gobalFunc(uint32_t coreDim, void* l2ctrl, void* stream, \
 void* tableAddr, uint32_t tableSize) \
 { \
 gobalFunc<<<coreDim, (rtL2Ctrl_t*)l2ctrl, stream>>>( \
 (half*)tableAddr, static_cast<TABLE_SIZE_INT64>(tableSize)); \
 }
#endif

上面代码中...对应__VA_ARGS__,表示在宏展开中将__VA_ARGS__替换成...的内容,此处的...可以表示表示一个或多个参数,但是常见的编译器也允许传递0个参数。再比如如下的代码

1
# define MYLOG(FormatLiteral, ...)  fprintf (stderr, "%s(%d): " FormatLiteral "\n", __FILE__, __LINE__, __VA_ARGS__)

对于下面的代码

1
MYLOG("Too many balloons %u", 42);

可以扩展为

1
fprintf (stderr, "%s(%d): Too many balloons %u\n", __FILE__, __LINE__, 42);

参考资料

comments powered by Disqus