I have a macro defined. But I need to change this value at run time depending on a condition. How can I implement this?
-
2Macros go through the pre-processor. They can't be modified during run-time. Is this a macro constant or a function macro that takes an argument? – Sep 27 '11 at 16:58
-
Macros are replaced prior to compilation, you can't affect them at runtime. What are you trying to do at runtime? – wkl Sep 27 '11 at 16:59
-
@birryree: Yes they are visible at runtime, how else would you use them? – Sep 27 '11 at 16:59
-
@0A0D - I meant that they are replaced prior to the compilation phase. Bad wording. – wkl Sep 27 '11 at 17:00
-
@0A0D: Nope, even the *compiler* doesn't see them, since the preprocessor expands them before that happens – Adam Batkin Sep 27 '11 at 17:00
-
no they are not! they are replaced inline once the preprocessor goes through the code and then the compilations starts! – long404 Sep 27 '11 at 17:00
-
@AdamBatkin: Yes, I know it was the wording that he used that he now acknowledges. – Sep 27 '11 at 17:01
-
there was nothing wrong with the wording. – Karoly Horvath Sep 27 '11 at 17:02
-
@lighthouse, what you are trying to do could probably be achieved with function pointers, or possibly even a switch-case statement. – Prof. Falken Sep 27 '11 at 17:03
-
7By the way, this question is a good one, so we can have it answered "no" on Stackoverflow. – Prof. Falken Sep 27 '11 at 17:03
-
@AmigableClarkKant: Yep, good question. Maybe should be in the C Faq? – Sep 27 '11 at 17:04
-
@0A0D: I saw the original one. (Is code *visible* at runtime??) – Karoly Horvath Sep 27 '11 at 17:13
-
@yi_H: Well, he said it was bad/not good wording. – Sep 27 '11 at 17:13
-
2Could you please provide example of your macro - it seems to me that you don't really need runtime-updateable-macro which does not make sense - but macro with one or more additional parameters. Or maybe redesign your code anyway.. – rudolf_franek Sep 27 '11 at 17:14
-
1# define SIZE 100 and in the code want to use it should be 50 in some case. Then what will be my step. Because I want to make a menu list depending on the SIZE. if(something) then create 50 menu else create SIZE menu currently what have I done is just create a variable and changed the variables value depending on the condition while it was updated by the marco before. – lighthouse Sep 27 '11 at 17:19
-
2@lighthouse, variables are the way to go then. – Prof. Falken Sep 27 '11 at 20:08
5 Answers
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.

- 35,429
- 19
- 126
- 178
-
What if the macro is assigned to a function which has the logic to get the desired value and returns? Wouldn't then the macro be able to return a dynamic value? – Harsh S. Jan 30 '19 at 05:24
-
@HarshS. If the macro resolves to a function, the preprocessor will replace the macro with the name of the function before the C++ file is compiled. The macro itself does not become "dynamic" in that case. Rather, the thing the macro resolves to is a thing that returns a runtime value. – fbrereto Jan 31 '19 at 19:50
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.

- 273,490
- 39
- 460
- 699
-
1hmm.. this answer says you can use macro as case label: http://stackoverflow.com/questions/1674032/static-const-vs-define-in-c/1674118#1674118 – Sep 27 '11 at 17:34
-
5@0A0D: it says, "you can't use a const int object as a case label (while a macro will work)". That's not intended to mean that *any* macro is a legal case label, or that using a macro somehow magically lets you use things in case labels that you otherwise could not. AndreyT is comparing `const int` objects with macros *defined as integer literals*. `case UNCHANGEABLE_VALUE:` is OK, `case CHANGEABLE_VALUE:` is not. – Steve Jessop Sep 27 '11 at 17:43
You can't. Macros are expanded by the Preprocessor, which happens even before the code is compiled. It is a purely textual replacement.
If you need to change something at runtime, just replace your macro with a real function call.

- 51,711
- 9
- 123
- 115
-
Could you please provide a simple example on how to replace it with a function call? – utvecklare Sep 11 '13 at 11:25
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)

- 12,442
- 4
- 49
- 101
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.

- 8,952
- 6
- 31
- 42