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

  1. Xref: sparky comp.lang.c:20266 comp.std.c:3430
  2. Newsgroups: comp.lang.c,comp.std.c
  3. Path: sparky!uunet!sangam!cmie!rs
  4. From: rs@cmie.ernet.in (Rajappa Iyer)
  5. Subject: Re: Preprocessor question
  6. Message-ID: <C1I4r2.2FF@cmie.ernet.in>
  7. Keywords: Preprocessor, string
  8. Organization: Centre for Monitoring Indian Economy, Bombay
  9. References: <1993Jan25.161425.27962@bnrmtl.bnr.ca>
  10. Date: Wed, 27 Jan 1993 07:28:14 GMT
  11. Lines: 35
  12.  
  13. karim@bnrmtl.bnr.ca (Karim Younes) writes:
  14.  
  15. >I am trying to have a #defined variable recognized by the
  16. >preprocessor inside a string. In other words, I want to
  17. >do something like this:
  18.  
  19. >#define JUNK 50
  20. >static char *junk = "JUNK ways to leave your lover."
  21.  
  22. The only ANSI way I can think of doing it is (yecch!):
  23.  
  24. #define JUNK "50"
  25. static char *junk = JUNK " ways to leave your lover."
  26.  
  27. Interestingly, I tried the following with gcc 2.2.2:
  28.  
  29. #define JUNK    50
  30. #define STR(x)    #x
  31.  
  32. char *junk = STR(JUNK) " ways to leave your lover."
  33.  
  34. junk expands to "JUNK ways to leave your lover." Which kind of
  35. makes me question the utility of the `#' preprocessor operator.
  36. K&R 2 says that "... replacement token sequence is preceded by #,
  37. string quotes (") are placed around the corresponding parameter
  38. and then both the # and the parameter identifier are replaced by
  39. the quoted argument."
  40.  
  41. In other words, in the above example `x' is replaced by `JUNK'
  42. and quoted---`JUNK' is not expanded. Is this indeed the correct
  43. behavior? If it is, I really think it severely restricts the
  44. utility of the `#' operator. Any comments, anyone?
  45. -- 
  46. Rajappa Iyer            rs@cmie.ernet.in (Preferred)
  47.                 riyer@shakti.ncst.ernet.in
  48.