如何在C中定义宏的宏?

维亚切斯拉夫

是否可以在C中定义宏的宏?通常,宏定义字符串的主体包含该语言的运算符。

但是,有时需要像在宏汇编程序中一样,在宏的主体中描述条件宏。我该怎么做?

含木

(1)您可以像在包含另一个宏的宏中那样定义宏的宏。

(2)但是,不能定义宏的宏#define INCLUDE #define STDH include <stdio.h>

这里,提供了一个非常简单的基于宏的Hello World程序(第一种情况)。MAIN是包含宏的宏HELLOWORLD

#include <stdio.h>
#define HELLOWORLD printf("Hello, World!") // Macro
#define MAIN int main() { HELLOWORLD; return 0; } // Macro containing HELLOWORLD macro
MAIN

被预处理为

#include <stdio.h>
int main() {
    HELLOWORLD;
    return 0;
}

然后将其预处理为

#include <stdio.h>
int main() {
    printf("Hello, World!");
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章