home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!think.com!rpi!crdgw1!rdsunx.crd.ge.com!bart!volpe
- From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
- Newsgroups: comp.lang.c
- Subject: Re: Preprocessor question
- Message-ID: <1993Jan27.195017.19661@crd.ge.com>
- Date: 27 Jan 93 19:50:17 GMT
- References: <1k44lqINNjr3@rs2.hrz.th-darmstadt.de> <1099@quirm.terminus.ericsson.se>
- Sender: volpe@bart (Christopher R Volpe)
- Reply-To: volpe@ausable.crd.ge.com
- Organization: GE Corporate Research & Development
- Lines: 35
- Nntp-Posting-Host: bart.crd.ge.com
-
- In article <1099@quirm.terminus.ericsson.se>, srl@terminus.ericsson.se (Steve Langstaff) writes:
- |> In article 1k44lqINNjr3@rs2.hrz.th-darmstadt.de, misar@rbg.informatik.th-darmstadt.de (Walter Misar) writes:
- |> : In article <mick.728072797@picus>, mick@dcs.bbk.ac.uk (Mick Farmer) writes:
- |> : > Hi,
- |> : >
- |> : > Karim asks how to get a preprocessor macro recognised inside
- |> : > a string. The answer, at least in ANSI C, is to use the
- |> : > stringising operator and string concatenation:
- |> : >
- |> : > #define JUNK 50
- |> : > static char *junk = #JUNK " ways to leave your lover"
- |> : ^^^^^
- |> : Nope, #token works only with #defines like in:
- |> : #define stringify(x) #x
- |> : #define str_exp(x) stringify(x) /* this extra level IS necessary */
- |> : static char *junk = str_exp(JUNK) "ways ....";
- |> :
- |> [snip]
- |>
- |> Why is the extra level necessary?
-
- Without the extra level, it quotes the macro argument as written. For
- example, the result of `stringify(JUNK)' using the above definitions
- would be `"JUNK"'. I.e., the argument JUNK gets quoted before being
- expanded. The extra indirection gives the argument JUNK a chance to
- be macro-expanded before being quoted. Hence, the result of `str_exp(JUNK)'
- is `"50"'.
-
- -Chris
-
- --
- ==================
- Chris Volpe
- G.E. Corporate R&D
- volpecr@crd.ge.com
-