Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!mit-eddie!genrad!decvax!minow From: minow@decvax.UUCP (Martin Minow) Newsgroups: comp.lang.c Subject: Re: #if inside #define -- easy to do, but is it a good idea? Message-ID: <163@decvax.UUCP> Date: Sun, 4-Oct-87 13:30:42 EDT Article-I.D.: decvax.163 Posted: Sun Oct 4 13:30:42 1987 Date-Received: Wed, 7-Oct-87 06:24:47 EDT References: <157@decvax.UUCP> <29339@sun.uucp> <10898@orchid.waterloo.edu> <7510@elsie.UUCP> Reply-To: minow@decvax.UUCP (Martin Minow) Organization: Digital Eq. Corp. - Merrimack NH. Lines: 53 Several responses suggested that the capabilities of # rescanning could be accomplished at execution time -- especially since modern compilers prune "dead-code" so a sequence like: #define DEBUG 0 #define DLOG(a) ((DEBUG) ? printf a : 0) ... DLOG(("%d", x)); generates no extraneous code. There are other needs, however. For example, here is example where it would be nice to be able to generate # commands inside of macros. It builds a symbol table of keywords: char *table[] = { "foo", # define SY_foo 0 "bar", # define SY_bar 1 NULL }; The macro would look something like #define SYM(name, n) \ (1) #name, \ (2) #define SY_##name n And would be used as SYM(foo, 0) SYM(bar, 1) Notes: (1) # is used here to "stringize" its argument. This means that SYM(define) or SYM(if) will not work. This problem is due to the ambiguity of the unary # operator. I suppose you could hack around it by writing (1) as "" ## #name, (2) Note that C doesn't give the programmer the tools to generate flexible counters. I.e, CPP is not a general purpose macro processor. Of course, K&R said this first. It should be pointed out that if you use command files, makefiles or their equivalent, it is very easy to build special purpose table generators that are automatically invoked when the appropriate module is compiled. The realworld (say, constructing menus for a window system) requires more complex table constructions than my simple extension would allow. Yacc and lex users have known this for years. Martin Minow decvax!minow