Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__COUNTER__ macro problems. Not displaying the value

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.

like image 322
user4362837 Avatar asked Oct 18 '25 05:10

user4362837


1 Answers

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
like image 137
2501 Avatar answered Oct 22 '25 07:10

2501



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!