简体   繁体   English

C预处理多遍

[英]C preprocessing multi pass

I'm a littlebit confused about the behaviour of preprocessing in c. 我对c中的预处理行为有些困惑。

    #include <stdio.h>

#define myMacro anotherMacro

#define anotherMacro 6

int main()
{
    int dummy = myMacro;
    printf("dummy = %d", dummy);

    return 0;
}

in the above code snippet, the result will 6. however the the macro expansion in the initial pass will replace "myMacro" by "anotherMacro". 在上面的代码片段中,结果将为6。但是,初始遍历中的宏扩展将由“ anotherMacro”替换“ myMacro”。 this is means that preprocessor will make a second pass to resolve "anotherMacro" to value 6. 这意味着预处理程序将第二遍将“ anotherMacro”解析为值6。

The preprocessor will make a second pass. 预处理器将进行第二遍处理。 He works through the source file line per line. 他逐行遍历源文件行。

So if he reaches the first define 所以如果他达到第一个定义

#define myMacro anotherMacro

he will replace all occurrences of myMacro with the string anotherMacro . 他将用字符串anotherMacro替换所有出现的myMacro

The file will look like this after the line is handled: 处理完该行后,文件将如下所示:

#include <stdio.h>

#define anotherMacro 6

int main()
{
    int dummy = anotherMacro;
    printf("dummy = %d", dummy);

    return 0;
}

Now the preprocessor could continue with the next #define 现在预处理器可以继续下一个#define
and replace every anotherMacro with the text 6 并用文本6替换所有其他

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM