2026-06-05 2:24 AM
Hello,
I am using STM32CubeIDE (eclipse) and was wondering if it is possible to get the constant calculated value of a macro.
For example
#define A 1
#define B 2
#define R A+B // R is now 3
#if R > 2
#warning R is greater
#else
#warning R is less 2
#endifI can only see 1+2 in the macroexpansion. But clearly the IDE has calculated the value and can evaluate the #if ... #else and gray out the unused part. So how can i access this calculated value?
2026-06-05 2:42 AM
A macro does not have a stored numeric value, it only performs text substitution. The preprocessor can evaluate the expression in #if, which is why the IDE can determine the active branch, but that result is not directly accessible as a macro value.
If you need a real compile-time constant, use an enum or a const instead.
Regards
/Peter
2026-06-05 2:45 AM
@Nickelgrass wrote:get the constant calculated value of a macro.?
Not sure what you mean by that ?
@Nickelgrass wrote:I can only see 1+2 in the macroexpansion.
That's to be expected; that's what macro expansion does - it is purely text substitution.
2026-06-05 2:56 AM
Thanks for the reply. But neither an enum or a const fits my application here. I just need to check if the calculated values are what they need to be.
2026-06-05 2:56 AM
Thanks for the reply. Yes I know it is only a text substitution. But I need to access the evaluated value of the macro to make sure it has the correct value.
2026-06-05 3:31 AM
2026-06-05 3:35 AM
Thanks but that only gives me the text and not the calculated value.
2026-06-05 3:39 AM
> A macro does not have a stored numeric value, it only performs text substitution. The preprocessor can evaluate the expression in #if, ...
And in addition to that, the preprocessor is a different "entity" than the C/C++ compiler, and uses different datatypes and ranges than the compiler. The result of preprocessor "calculations" might not be what you expect.
> If you need a real compile-time constant, use an enum or a const instead.
Thus I would definitely second that.
2026-06-05 3:39 AM - edited 2026-06-05 3:45 AM
Hmm maybe the upstream Eclipse CDT can calculate "constexpr" and "consteval" expressions for new C++ standard? If not yet, it should be proposed, and by the same occasion add evaluation of const expressions in C macros :)
2026-06-05 3:40 AM - edited 2026-06-05 3:43 AM
That still just shows the macro expansion - it doesn't evaluate the result of the expression after expansion.
@Nickelgrass I think you're just going to have to do the evaluation manually.
Or write some test code that will do it for you ...
#define A 1
#define B 2
#define R A+B // R is now 3 (sic)
printf( "A=%d; B=%d; R=%d", A, B, R );
Note that this comment is incorrect:
#define R A+B // R is now 3R is not 3.
R just expands to A+B
R does not have the numerical value 3
And, for safety, it should be
#define R (A+B)
We’re moving the ST Community to a new platform to give you a better and more reliable community experience.