I am trying to use the __COUNTER__ macro to generate unique variable names in my code. But the macro doesn't seem to work. I may be using it the wrong way. Please provide me pointers or suggestion to what I am doing wrong.
#define DUMB_MACRO() ht##__COUNTER__
should give me ht0,ht1....
The way I am calling it in the main file is
DUMB_MACRO();
But the compiler says it doesn't resolve the symbol ht__COUNTER__ if I try using ht0 variable.
I also tried using the __CONCAT macro but I cannot pass variable into it.
For example:
__CONCAT(ht,1) works and gives me ht1 but __CONCAT(ht,i) where i is a variable holding saying the value 1 doesn't work because its value is not known at compile time.
You have to expand the macro:
#define MACRO3(s) ht##s
#define MACRO2(s) MACRO3(s)
#define MACRO MACRO2(__COUNTER__)
int MACRO ; //ht0
int MACRO ; //ht1
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