简体   繁体   English

预处理器和宏

[英]Preprocessor and macros

#include <stdio.h>
#define macro(n, a, i, m) m##a##i##n
#define MAIN macro(n, a, i, m)
int MAIN()
{

    printf("GeeksQuiz");

    return 0;
}

Hello people In this question the output is geeksquiz as it says that main is first replaced by macro then again replaced.大家好,在这个问题中,输出是 geeksquiz,因为它说 main 首先被宏替换,然后再次被替换。 Where does the actual replacement take place ?实际更换在哪里进行? Isn't it just defining functions ?不就是定义函数吗? Thank you谢谢

This takes place in the fourth phase of translation (known as the preprocessing phase) by the compiler.这发生在编译器的第四个翻译阶段(称为预处理阶段)。

You can read up on " Phases of Translation "您可以阅读“翻译阶段

If there was some other name of the function other than macro , would it have done the same or defined it as different functions如果除了宏之外还有函数的其他名称,它是否会执行相同的操作或将其定义为不同的函数?

Yes.是的。 "macro" can be changed to some other name. “宏”可以更改为其他名称。

#define some_other_name(n, a, i, m) m##a##i##n
#define MAIN some_other_name(n, a, i, m)

How do we know when the main function is getting replaced.我们如何知道主函数何时被替换。 In cases ive seen that they are defined as separate functions, is it due to macro or MAIN ?如果我看到它们被定义为单独的函数,是由于宏还是 MAIN ? ( Sorry new to C here ) (对不起,这里是 C 的新手)

Because MAIN is defined as a MACRO and the MACRO expansion is resulting in main .因为 MAIN 被定义为 MACRO 并且 MACRO 扩展导致main

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

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