home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20176 < prev    next >
Encoding:
Text File  |  1993-01-25  |  2.0 KB  |  67 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gossip.pyramid.com!decwrl!pacbell.com!tandem!UB.com!pippen.ub.com!rfries
  3. From: rfries@sceng.ub.com (Robert Fries)
  4. Subject: Re: Preprocessor question
  5. Message-ID: <rfries.178@sceng.ub.com>
  6. Keywords: Preprocessor, string
  7. Sender: news@pippen.ub.com (The Daily News)
  8. Nntp-Posting-Host: 128.203.1.151
  9. Organization: Ungermann Bass
  10. References: <1993Jan25.161425.27962@bnrmtl.bnr.ca>
  11. Date: Mon, 25 Jan 1993 17:34:17 GMT
  12. Lines: 53
  13.  
  14. In article <1993Jan25.161425.27962@bnrmtl.bnr.ca> karim@bnrmtl.bnr.ca (Karim Younes) writes:
  15.  
  16. >I am trying to have a #defined variable recognized by the
  17. >preprocessor inside a string. In other words, I want to
  18. >do something like this:
  19.  
  20. >#define JUNK 50
  21. >static char *junk = "JUNK ways to leave your lover."
  22.  
  23. >Now the preprocessor ignores JUNK, because it is inside the double
  24. >quotes. Is there any way to get around this ? Please respond 
  25. >by e-mail or by followup, I will post a summary if needed. Thanks.
  26.  
  27. >-- 
  28. >Karim "Kouki" Younes                      Bell-Northern Research
  29. >karim@bnr.ca                              Montreal, Canada
  30.  
  31.  
  32. Adjacent strings are concatenated, at least in ANSI C.
  33. So you can do:
  34.  
  35. #define JUNK "50"
  36. static char *junk = JUNK" ways to leave your lover."
  37.  
  38. During pre-processing, the definition of junk becomes:
  39. static char *junk = "50"" ways to leave your lover."
  40.  
  41. The two string are concatenated, so junk becomes a pointer to the following 
  42. string:
  43. "50 ways to leave your lover."
  44.  
  45. In your example, you wanted to replace the first word in a string.
  46. If you wanted to replace a word somewhere in the middle of the string,
  47. you could do the following:
  48.  
  49. #define JUNK "50"
  50. char *junk = "There are "JUNK" ways to leave your lover."
  51.  
  52. In this case three strings are concatenated into one.
  53.  
  54. I hope this helps.
  55.  
  56. Robert
  57.  
  58.  
  59. //////////////////////////////////////////////////////////////////////////
  60. Robert Fries
  61. Ungermann-Bass Inc.
  62.  
  63. DISCLAIMER:
  64.     Opinions contained herein are my own, and are not necessarily those
  65.     of Ungermann-Bass.
  66. //////////////////////////////////////////////////////////////////////////
  67.