inquisitive
inquisitive

Reputation: 1680

Variable names in C

In preprocessors, we can have switch between macros like,

#define BUFF(n) BUFF_##n

So, BUFF(1) would get replaced by BUFF_1, BUFF(2) would get replaced by BUFF_2 and song

Can this be applicable to C variables? i.e., choosing between similar variables dynamically. I understand it is a weird situation and can be handled using arrays or any other constructs.. but the situation demands me such situation.. could u plz help with this.. thanks in advance

Upvotes: 1

Views: 509

Answers (2)

sharptooth
sharptooth

Reputation: 170469

Yes, you can use that macro to apply BUFF_ to just anything. The preprocessor will expand macros and then the compiler will try to compile the result. The latter might fail, since if you use BUFF(+) you get BUFF_+ and that's not a valid variable name.

Upvotes: 4

Andreas Kraft
Andreas Kraft

Reputation: 3843

Sure, you can do this. preprocessor macros are just text replacements that are done to the code before compilation. You can't do this during runtime, though.

Upvotes: 3

Related Questions