Jeff Weisberg on Thu, 21 Nov 2002 22:30:05 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] C #define wizardry question


| I'm trying to write a C macro like this:
| 
|     #define set(x) { x##_ = 1; printf("The variable "##x##"_ is set.\n");
|  }
| 
| So then I want to be able to say
| 
|     set(dog);
|     set(cat);
| 
| and have it expand to
| 
|     { dog_ = 1; printf("The variable dog_ is set.\n"); };
|     { cat_ = 1; printf("The variable cat_ is set.\n"); };
| 
| Unfortunately, the incantation in the printf doesn't work.


assuming ANSI C, you want #x which quotes x, yielding "dog"

#define set(x) { x##_ = 1; printf("The variable " #x "_ is set.\n"); }


	--jeff

_________________________________________________________________________
Philadelphia Linux Users Group        --       http://www.phillylinux.org
Announcements - http://lists.netisland.net/mailman/listinfo/plug-announce
General Discussion  --   http://lists.netisland.net/mailman/listinfo/plug