home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20301 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  1.8 KB

  1. Path: sparky!uunet!think.com!rpi!crdgw1!rdsunx.crd.ge.com!bart!volpe
  2. From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Preprocessor question
  5. Message-ID: <1993Jan27.195017.19661@crd.ge.com>
  6. Date: 27 Jan 93 19:50:17 GMT
  7. References: <1k44lqINNjr3@rs2.hrz.th-darmstadt.de> <1099@quirm.terminus.ericsson.se>
  8. Sender: volpe@bart (Christopher R Volpe)
  9. Reply-To: volpe@ausable.crd.ge.com
  10. Organization: GE Corporate Research & Development
  11. Lines: 35
  12. Nntp-Posting-Host: bart.crd.ge.com
  13.  
  14. In article <1099@quirm.terminus.ericsson.se>, srl@terminus.ericsson.se (Steve Langstaff) writes:
  15. |> In article 1k44lqINNjr3@rs2.hrz.th-darmstadt.de, misar@rbg.informatik.th-darmstadt.de (Walter Misar) writes:
  16. |> : In article <mick.728072797@picus>, mick@dcs.bbk.ac.uk (Mick Farmer) writes:
  17. |> : > Hi,
  18. |> : > 
  19. |> : > Karim asks how to get a preprocessor macro recognised inside
  20. |> : > a string.  The answer, at least in ANSI C, is to use the
  21. |> : > stringising operator and string concatenation:
  22. |> : > 
  23. |> : > #define JUNK 50
  24. |> : > static char *junk = #JUNK " ways to leave your lover"
  25. |> :                       ^^^^^
  26. |> : Nope, #token works only with #defines like in:
  27. |> : #define stringify(x) #x
  28. |> : #define str_exp(x) stringify(x)        /* this extra level IS necessary */
  29. |> : static char *junk = str_exp(JUNK) "ways ....";
  30. |> : 
  31. |> [snip]
  32. |> 
  33. |> Why is the extra level necessary?
  34.  
  35. Without the extra level, it quotes the macro argument as written. For
  36. example, the result of `stringify(JUNK)' using the above definitions
  37. would be `"JUNK"'. I.e., the argument JUNK gets quoted before being
  38. expanded. The extra indirection gives the argument JUNK a chance to
  39. be macro-expanded before being quoted. Hence, the result of `str_exp(JUNK)'
  40. is `"50"'.
  41.  
  42. -Chris
  43.  
  44. -- 
  45. ==================
  46. Chris Volpe
  47. G.E. Corporate R&D
  48. volpecr@crd.ge.com
  49.