【发布时间】:2020-12-28 17:24:14
【问题描述】:
在 C 中尝试在枚举中编写宏时。 有时我们必须在宏定义上面写宏的名字,为什么?
#include <stdio.h>
enum month {
MSD //If I remove or comment this line, code does not work, why?
#define MSD 7
};
void main() {
printf("%d\n", MSD);
}
如果我们删除第 5 行代码,即“MSD”。我们得到错误 空枚举无效。 为什么会这样,谁能解释一下?
【问题讨论】:
-
当第 5 行不存在时,您认为 应该 发生什么?
-
记住预处理器的工作原理:宏在代码传递给编译器之前被扩展。想想编译器实际上看到了什么。没有“宏变量”之类的东西,也不是“声明”。
-
打开,并阅读,你的编译器诊断。
标签: c enums nested macros c-preprocessor