Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!apple!julius.cs.uiuc.edu!wuarchive!udel!princeton!phoenix.Princeton.EDU!pfalstad From: pfalstad@phoenix.Princeton.EDU (Paul John Falstad) Newsgroups: comp.lang.c Subject: Re: Embedding macro values in strings Message-ID: <5196@idunno.Princeton.EDU> Date: 11 Jan 91 08:44:52 GMT References: <1991Jan10.200806.12745@grep.co.uk> Sender: news@idunno.Princeton.EDU Organization: Princeton University, Princeton, New Jersey Lines: 37 In article <1991Jan10.200806.12745@grep.co.uk> frank@grep.co.uk (Frank Wales) writes: >For example, suppose I have foo(__LINE__); in my source, and I want that >to expand to bar("Line n"); where n is substituted with the actual value >of __LINE__ at that point. Trying > > #define foo(n) bar("Line "#n) > >and invoking as foo(10); yields bar("Line 10"); , whereas foo(__LINE__); >yields bar("Line __LINE__"); . You realize, of course, that foo(10) would not actually produce: bar("line 10"), but will produce: bar("line " "10") wish is concatenated into a single string in a later phase of compilation. Just clarifying that. Anyway, adding another level of macro expansion does the trick: #define foo(n) bar("Line "fee(n)) #define fee(X) #X foo(10) foo(__LINE__) This produces the intended result (using gcc): bar("Line ""10" ) bar("Line ""4" ) -- Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD "We could nuke Baghdad into glass, wipe it with Windex, tie fatback on our feet and go skating." - Air Force Times columnist Fred Reed