1

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

Konamiman
  • 49,681
  • 17
  • 108
  • 138
inquisitive
  • 1,558
  • 4
  • 16
  • 22

2 Answers2

4

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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Quite handy for subroutines too. I can't recall details but I'm fairly sure in my past life we had a real life application of this construct, although to be fair some of our coding practices were a bit off the wall... – Tony van der Peet Dec 09 '09 at 09:23
3

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.

Andreas Kraft
  • 3,754
  • 1
  • 30
  • 39