celiazou0914
celiazou0914

Reputation: 75

How to define multiple similar macro using "##"?

I don't know if there're already some guy asking the same question, but I couldn't find it with the Advanced Search here with [c] [macro] "##".

I want to define multiple macros as follows:

#define CHANNEL_0  0
#define CHANNEL_1  1
...
#define CHANNEL_31 31

Can I use this symbol ## to do it in a simple way? And How? Or maybe there're some ways?

Thanks!

Upvotes: 3

Views: 173

Answers (1)

KBart
KBart

Reputation: 1598

I don't think that "##" is the best solution here. Why not just use enum? I see no reason you could not use it if only numbers from 0 to 31 are needed.

enum eChannel {
    Channel0, /* evaluates to 0 */
    Channel1, /* evaluates to 1 */
    ...
    Channel31 /* evaluates to 31 */
};

And the usage is the same as with #defines

if(channel == Channel1) do_smth();

Upvotes: 3

Related Questions