cancel
Showing results for 
Search instead for 
Did you mean: 

Get calculated values from defined constants in macro

Nickelgrass
Senior II

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
#endif

I 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? 

12 REPLIES 12
Peter BENSCH
ST Employee

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

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Andrew Neil
Super User

@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.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

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. 

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.

Pavel A.
Super User

PavelA_0-1780655510822.png

 

Thanks but that only gives me the text and not the calculated value.

> 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.

Pavel A.
Super User

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 :)

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 3

R 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)

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.