Linux内核中代码中\的作用

参考内核代码队列入队的实现代码,在宏中用\的意义在哪?

/**
 * kfifo_in - put data into the fifo
 * @fifo: address of the fifo to be used
 * @buf: the data to be added
 * @n: number of elements to be added
 *
 * This macro copies the given buffer into the fifo and returns the
 * number of copied elements.
 *
 * Note that with only one concurrent reader and one concurrent
 * writer, you don't need extra locking to use these macro.
 */
#define    kfifo_in(fifo, buf, n) \
({ \
    typeof((fifo) + 1) __tmp = (fifo); \
    typeof((buf) + 1) __buf = (buf); \
    unsigned long __n = (n); \
    const size_t __recsize = sizeof(*__tmp->rectype); \
    struct __kfifo *__kfifo = &__tmp->kfifo; \
    if (0) { \
        typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
        __dummy = (typeof(__buf))NULL; \
    } \
    (__recsize) ?\
    __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
    __kfifo_in(__kfifo, __buf, __n); \
})
阅读 3.4k
3 个回答

连接多行为一行,可以将较长的宏定义分成若干行.
如:

#include <stdio.h>
#define MAX 3\
2

int main(void) {
    int b = MAX;
    printf("%d\n", b);
    return 0;
}

最终, MAX会被替换成32, 而不是3.

楼上说的对,不过不只局限于宏定义,任何地方加了 \ 然后换行,都会产生这个效果。

反斜杠的意在于方便人阅读。

因为 C 语言中 #define 语句的作用域是一行。
代码中使用函数宏,内容多,使用 分行,方便人阅读。对编译器都一样的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进