在定义中看不到的枚举常量 [英] Constant in enum not seen in define

查看:97
本文介绍了在定义中看不到的枚举常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C

enum {
     MYVAR = 1
};

#ifdef MYVAR
#define VAR 1
#else
#define VAR 2
#endif

printf("VAR = %d", VAR);

在这种情况下,它将打印 VAR = 2。

in this case it will prints "VAR = 2".

有什么办法让预处理器看到枚举中的定义吗?

Is there any way to get preprocessor see the definition in enum?

推荐答案

否,这是不可能的。 #ifdef #if 是预处理程序的一部分,它们在编译器理解的部分之前完成其运行。 枚举 s。

No, this is not possible. #ifdef and #if are part of preprocessor, which completes its run before the portion of the compiler that "understands" enums.

#ifdef 仅适用于预处理器常量(即用 #define 定义的内容,或通过 -DMYVAR = 123 选项)。

#ifdef works only with preprocessor constants (i.e. things defined with #define, or passed to the compiler on the command line, say, with a -DMYVAR=123 option).

#if 适用于整数和字符常量以及预处理程序常量。所有不是预处理器常量的标识符都被认为是未定义的,并且在评估 #if 条件时,将其解释为零。其中包括枚举常量。

#if works with integer and character constants, and preprocessor constants. All identifiers which are not preprocessor constants are considered undefined, and interpreted as if they were zeros when evaluating #if conditions. This includes enum constants.

这篇关于在定义中看不到的枚举常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆