C语言 在 C 中运行时更改宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7572872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing a macro at runtime in C
提问by lighthouse
I have a macro defined. But I need to change this value at run time depending on a condition. How can I implement this?
我定义了一个宏。但是我需要在运行时根据条件更改此值。我该如何实施?
采纳答案by fbrereto
Macros are replaced by the preprocessor by their value before your source file even compiles. There is no way you'd be able to change the value of the macro at runtime.
在源文件编译之前,宏会被预处理器替换为它们的值。您无法在运行时更改宏的值。
If you could explain a little more about the goal you are trying to accomplish undoubtedly there is another way of solving your problem that doesn't include macros.
如果您可以进一步解释一下您要实现的目标,无疑还有另一种不包含宏的方法可以解决您的问题。
回答by Steve Jessop
You can't change the macro itself, i.e. what it expands to, but potentially you can change the value of an expressioninvolving the macro. For a very silly example:
您不能更改宏本身,即它扩展到的内容,但您可能可以更改涉及宏的表达式的值。对于一个非常愚蠢的例子:
#include <stdio.h>
#define UNCHANGEABLE_VALUE 5
#define CHANGEABLE_VALUE foo
int foo = 5;
int main() {
printf("%d %d\n", UNCHANGEABLE_VALUE, CHANGEABLE_VALUE);
CHANGEABLE_VALUE = 10;
printf("%d %d\n", UNCHANGEABLE_VALUE, CHANGEABLE_VALUE);
}
So the answer to your question depends on what kind of effect you want your change to have on code that uses the macro.
因此,您的问题的答案取决于您希望您的更改对使用宏的代码产生什么样的影响。
Of course 5is a compile-time constant, while fooisn't, so this doesn't work if you planned to use CHANGEABLE_VALUEas a caselabel or whatever.
当然5是编译时常量,而foo不是,所以如果您打算CHANGEABLE_VALUE用作case标签或其他任何东西,这将不起作用。
Remember there are two (actually more) stages of translation of C source. In the first (of the two we care about), macros are expanded. Once all that is done, the program is "syntactically and semantically analyzed", as 5.1.1.2/2 puts it. These two steps are often referred to as "preprocessing" and "compilation" (although ambiguously, the entire process of translation is also often referred to as "compilation"). They may even be implemented by separate programs, with the "compiler" running the "preprocessor" as required, before doing anything else. So runtime is way, way too late to try to go back and change what a macro expands to.
请记住,C 源代码的翻译有两个(实际上更多)阶段。在第一个(我们关心的两个)中,宏被扩展。一旦完成所有这些,程序就会“从语法和语义上进行分析”,正如 5.1.1.2/2 所说。这两个步骤常被称为“预处理”和“编译”(虽然含糊不清,整个翻译过程也常被称为“编译”)。它们甚至可以由单独的程序实现,在执行任何其他操作之前,“编译器”根据需要运行“预处理器”。因此,运行时是一种方式,尝试返回并更改宏扩展到的内容为时已晚。
回答by Adam Batkin
You can't. Macros are expanded by the Preprocessor, which happens even before the code is compiled. It is a purely textual replacement.
你不能。宏由Preprocessor扩展,甚至在代码编译之前就发生了。它是纯文本替换。
If you need to change something at runtime, just replace your macro with a real function call.
如果您需要在运行时更改某些内容,只需将您的宏替换为真正的函数调用。
回答by Geoffroy
You can't.
你不能。
As a macro is resolved by the preprocessor before the compilation itself, its content is directly copied where you use it.
由于宏在编译之前由预处理器解析,因此它的内容会直接复制到您使用它的地方。
You can still use parameters to insert a conditional statement depending on what you want, or use a call-scope accessible variable.
您仍然可以根据需要使用参数插入条件语句,或者使用调用范围可访问的变量。
If you want to change a single value, better use global scope variable, even if such behavior is discouraged. (as the intensive use of macro)
如果您想更改单个值,最好使用全局范围变量,即使不鼓励这种行为。(作为宏的密集使用)
回答by morningstar
Depending on what you want to do, you might do it several ways.
根据您想要做什么,您可以通过多种方式进行。
Global variable instead of macro
全局变量而不是宏
// someincludefile.h
extern static int foo;
// someincludefile.c
static int foo = 5;
// someotherfile.c
#include "someincludefile.h"
printf("%d\n", foo); // >> 5
foo = -5;
printf("%d\n", foo); // >> -5
Condition you can toggle
您可以切换的条件
// someincludefile.h
extern static int condition;
#define FOO1 (5)
#define FOO2 (-5)
#define FOO (condition ? (FOO1) : (FOO2))
// someincludefile.c
static int condition = 1;
// someotherfile.c
#include "someincludefile.h"
printf("%d\n", FOO); // >> 5
condition = 0;
printf("%d\n", FOO); // >> -5
Condition that's locally and dynamically evaluated
本地和动态评估的条件
// someincludefile.h
#define CONDITION (bar >= 0)
#define FOO1 (5)
#define FOO2 (-5)
#define FOO ((CONDITION) ? (FOO1) : (FOO2))
// someotherfile.c
#include "someincludefile.h"
int bar = 1;
printf("%d\n", FOO); // >> 5
bar = -1;
printf("%d\n", FOO); // >> -5
In that last one the CONDITION will be evaluated as if its code were in your local scope, so you can use local variables and/or parameters in it, but you can also use global variables if you want.
在最后一个中, CONDITION 将被评估,就好像它的代码在您的本地范围内一样,因此您可以在其中使用本地变量和/或参数,但如果您愿意,您也可以使用全局变量。

