user4362837
user4362837

Reputation: 87

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

Upvotes: 4

Views: 1132

Answers (1)

2501
2501

Reputation: 25753

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

Upvotes: 6

Related Questions