I have a macro defined. But I need to change this value at run time depending on a condition. How can I implement this?
The process to redefine a Macro is:Macro must be defined. When, you want to redefine the Macro, first of all, undefined the Macro by using #undef preprocessor directive. And, then define the Macro again by using #define preprocessor directive.
A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).
The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.
Function-like macros can take arguments, just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.
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.
You can't change the macro itself, i.e. what it expands to, but potentially you can change the value of an expression involving 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 5
is a compile-time constant, while foo
isn't, so this doesn't work if you planned to use CHANGEABLE_VALUE
as a case
label or whatever.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With