如何有条件地定义常量

发布于 2024-09-06 16:28:05 字数 455 浏览 5 评论 0原文

我想在我的 C 文件中定义一些常量。

它的汇编代码是这样的:

Const1 = 0

Const2 = 0

IF Condition1_SUPPORT

    Const1 = Const1 or (1 shl 6)

    Const2 = Const2 or (1 shl 3)

ENDIF

IF Condition2_SUPPORT

    Const1 = Const1 or (1 shl 5)

    Const2 = Const2 or (1 shl 2)

ENDIF

你能告诉我最简单的实现方法吗?

而且它应该足够灵活,因为我的常量条件的数量都超过10个。

看到前三个答案后,我想我需要解释更多; 我想知道的是如何根据以前的值重新定义我的常量。

I want to define some constants in my C file.

Its assembly code like this:

Const1 = 0

Const2 = 0

IF Condition1_SUPPORT

    Const1 = Const1 or (1 shl 6)

    Const2 = Const2 or (1 shl 3)

ENDIF

IF Condition2_SUPPORT

    Const1 = Const1 or (1 shl 5)

    Const2 = Const2 or (1 shl 2)

ENDIF

Could you tell me the simplest way to implement this?

And it should be flexible enough because the number of both my constants and conditions is over 10.

After seeing the first three answers, I guess I need to explain more;
What I want to know is how to redefine my constant based on its previous value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九命猫 2024-09-13 16:28:05

您可以使用预处理指令来执行此操作:

#if Condition1_SUPPORT
    #define Const1 (1 << 6)
    // ...
#elif Condition2_SUPPORT
    #define Const1 (1 << 5)
    // ...
#endif

要解决问题的编辑:您无法根据宏的先前值重新定义宏。宏一次只能有一个值,并且其替换列表仅在调用时计算,而不是在定义时计算。例如,这是不可能的:

#define A 10
#define A A + 10

首先,这是对宏的非法重新定义:当处理第二行时,A已经被定义为宏,因此不能用不同的宏重新定义它。替换(您必须首先#undef宏名称)。

其次,如果这是合法的(许多编译器确实接受它),则第二行在调用时将计算为 A + 10,而不是 10 + 1020 如您所愿:当第二个宏定义可以被调用时,第一个定义不再存在。

但是,您可以使用不同的名称,如下所示:

#define INITIAL_A 10
#define A INITIAL_A + 10

您应该考虑从 权威 C 书籍指南和列表;其中任何一个都将详细介绍使用预处理指令可以完成的工作。

You can do so using preprocessing directives:

#if Condition1_SUPPORT
    #define Const1 (1 << 6)
    // ...
#elif Condition2_SUPPORT
    #define Const1 (1 << 5)
    // ...
#endif

To address the edit to the question: you can't redefine a macro based on its previous value. A macro can only have one value at a time and its replacement list is only evaluated when it is invoked, not when it is defined. For example, this is not possible:

#define A 10
#define A A + 10

First, it is an illicit redefinition of the macro: when the second line is handled, A is already defined as a macro, and so it cannot be redefined with a different replacement (you have to #undef the macro name first).

Second, were this licit (and many compilers do accept it), the second line, when invoked, would evaluate to A + 10, not 10 + 10 or 20 as you want: by the time the second macro definition could be invoked, the first definition no longer exists.

You can, however, use different names, like so:

#define INITIAL_A 10
#define A INITIAL_A + 10

You should consider getting one of the introductory books from The Definitive C Book Guide and List; any of them would cover what can be accomplished using the preprocessing directives in some detail.

黑寡妇 2024-09-13 16:28:05

一旦你分配了常量,你就不能重新定义它的值——如果可以的话,它就不会是常量了,不是吗?由于看起来您正在尝试根据预处理器标志在常量中设置各个位,因此您可以#define为每个条件的贡献分隔常量,然后在最后构建最终值:

#if Condition1_SUPPORT
#  define Const1_Cond1_Bit (1 << 6)
#  define Const2_Cond1_Bit (1 << 3)
#else
#  define Const1_Cond1_Bit (0)
#  define Const2_Cond1_Bit (0)
#endif

#if Condition2_SUPPORT
#  define Const1_Cond2_Bit (1 << 5)
#  define Const2_Cond2_Bit (1 << 2)
#else
#  define Const1_Cond2_Bit (0)
#  define Const2_Cond2_Bit (0)
#endif

#define Const1 (Const1_Cond1_Bit | Const1_Cond2_Bit)
#define Const2 (Const2_Cond1_Bit | Const2_Cond2_Bit)

然后您可以#undef 所有中间常量,如果命名空间污染是一个问题。

You can't redefine the value of a constant once you assign it -- if you could, it wouldn't be a constant, would it? Since it looks like you're trying to set various bits in a constant based on preprocessor flags, you could #define separate constants for each condition's contribution, then build the final value at the end:

#if Condition1_SUPPORT
#  define Const1_Cond1_Bit (1 << 6)
#  define Const2_Cond1_Bit (1 << 3)
#else
#  define Const1_Cond1_Bit (0)
#  define Const2_Cond1_Bit (0)
#endif

#if Condition2_SUPPORT
#  define Const1_Cond2_Bit (1 << 5)
#  define Const2_Cond2_Bit (1 << 2)
#else
#  define Const1_Cond2_Bit (0)
#  define Const2_Cond2_Bit (0)
#endif

#define Const1 (Const1_Cond1_Bit | Const1_Cond2_Bit)
#define Const2 (Const2_Cond1_Bit | Const2_Cond2_Bit)

You can then #undef all the intermediate constants, if namespace pollution is a concern.

似最初 2024-09-13 16:28:05

您可以使用预处理器宏有条件地定义常量变量,例如

#if SOME_CONDITION
    const int my_constant = 10;
#else
    const int my_constant = 5;
#endif

You can use pre processor macros to conditionally define a constant variable, like

#if SOME_CONDITION
    const int my_constant = 10;
#else
    const int my_constant = 5;
#endif
恏ㄋ傷疤忘ㄋ疼 2024-09-13 16:28:05

在 C 中,您可以使用预处理器宏来完成此任务:

#ifdef COND1_SUPPORT
#define CONST1 CONST1_VAL1
#define CONST2 CONST2_VAL1
#elif COND2_SUPPORT
#define CONST1 CONST1_VAL2
#define CONST2 CONST2_VAL2
#endif

In C, you would use preprocessor macros to accomplish that:

#ifdef COND1_SUPPORT
#define CONST1 CONST1_VAL1
#define CONST2 CONST2_VAL1
#elif COND2_SUPPORT
#define CONST1 CONST1_VAL2
#define CONST2 CONST2_VAL2
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文