B.B.
B.B.

Reputation: 57

Evaluate constant before displaying it in preprocessor

On this site I found tricks to display constant values in preprocessor, like:

#define VALUE_TO_STRING(x) #x
#define VALUE(x) VALUE_TO_STRING(x)
#define VAR_NAME_VALUE(var) #var "="  VALUE(var)
#define A 5
#pragma display(VAR_NAME_VALUE(A))

that generates the output A=5

and they work well, but when I need to evaluate something like:

#define A 5
#define B (A*5)
#pragma display(B)

it displays the output B=5*5.

What if I wanted to display B=25? How should I write the preprocessor directive to do this?

Upvotes: 2

Views: 139

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

You can not, the macro replacement mechanism does not involve doing calculations, but you can take a look to GNU M4:

Besides just doing macro expansion, m4 has built-in functions for including named files, running shell commands, doing integer arithmetic, manipulating text in various ways, performing recursion, etc....

at built-in eval http://www.gnu.org/software/m4/manual/m4.html#Eval

Upvotes: 1

Related Questions