Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
条件导数-#在C中定义_C_Linux - Fatal编程技术网

条件导数-#在C中定义

条件导数-#在C中定义,c,linux,C,Linux,我在读一些C代码,被卡在这里了 下面是头文件中的代码片段 #if NUMREPS == 0 #define REPEAT(line) REPEAT0(line); #elif NUMREPS == 16 #define REPEAT(line) REPEAT16(line); #endif 以及标识符repeat16(行)的导数定义如下: #define REPEAT16(line) \ line;\ line;\ line;\

我在读一些
C
代码,被卡在这里了

下面是头文件中的代码片段

#if NUMREPS == 0
        #define REPEAT(line) REPEAT0(line);
#elif NUMREPS == 16
        #define REPEAT(line) REPEAT16(line);
#endif
以及标识符
repeat16(行)的导数定义如下:

#define REPEAT16(line) \
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;\
    line;
这个代码片段到底做什么?
我利用这一点来理解代码,预处理器是编译过程中的一个步骤,在实际编译之前运行。它对宏所做的只是用宏体替换宏调用。因此,当预处理器看到
REPEAT16
的“调用”时,它只需将其替换为宏的参数,在主体中重复16次

参数
line
正是传递给宏的参数,因此,如果您像这样调用它

REPEAT16(printf("hello\n"))
然后编译器看到的代码将

printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n"); printf("hello\n");
宏体中的
\
字符只是告诉预处理器当前行继续下一行。因此,整个正文将是一行。

一行末尾的反斜杠(
\
)将由预处理器替换为以下行

编写多行宏定义是一种常见的方法,因为替换文本必须位于单个“逻辑”行上,所以使用行继续符使其更易于阅读

在使用宏定义整个函数时,经常使用此选项:

#define INT_RETURNER(x)  int return_ ## x (void)\
                         {\
                           return x;\
                         }

INT_RETURNER(4711)

int main(void)
{
  printf("%d!\n", return_4711());
}

哪个部分让你困惑?line;\`使用
gcc-C-E sourcecode.C>sourcecode.i
获取预处理表单
sourcecode.i
。然后用寻呼机(
less sourcecode.i
)或编辑器(
emacs sourcecode.i
)查看它的内部。@basilestrynkevitch:它是一个头文件(扩展名:.h),而不是(.c),但通常在一些
sourcecode.c
中包含
头文件.h
,这就是我所说的
sourcecode.c
这只是一个天真的问题:这是否意味着它假定
没有新行字符?
可能值得注意的是
if(false)REPEAT16(printf(“hello\n”)
将打印
hello
15次。@user2015933否,预处理器不会添加任何换行符。宏体必须是单行,而“`的用法是将多行宏体连接到一行中。为什么替换文本需要位于一行逻辑线上?@user2015933,因为宏不能跨越多个逻辑行。我相信标准中有一些更精确的语言,但这是最基本的。