home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 13106 < prev    next >
Encoding:
Internet Message Format  |  1992-09-02  |  1.1 KB

  1. Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!wirzeniu
  2. From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: printf question
  5. Message-ID: <1992Sep2.124131.13678@klaava.Helsinki.FI>
  6. Date: 2 Sep 92 12:41:31 GMT
  7. References: <1992Sep01.214725.6334@ima.isc.com> <18260@acorn.co.uk>
  8. Organization: University of Helsinki
  9. Lines: 27
  10.  
  11. jcoxhead@acorn.co.uk (Jonathan Coxhead) writes:
  12. >         The C preprocessor has no way to evaluate-and-stringise a constant
  13. >      expression.
  14. >
  15. >but it does!
  16. >
  17. >[ C code deleted, but it's almost the same as the code below, but with
  18. >  WIDTH defined as 8 only --liw]
  19.  
  20. Input to preprocessor:
  21.     #define STR_(s) #s
  22.     #define STR(s)  STR_ (s)
  23.     #define WIDTH 8+2
  24.     printf ("%" STR (WIDTH) "x", n);
  25.  
  26. Output from preprocessor (with empty lines removed):
  27.     # 1 "foo.c"
  28.     printf ("%"  "8+2"   "x", n);
  29.  
  30. Nope, the C preprocessor does not evaluate 8+2 into 10.
  31.  
  32. (Not that I'm not saying that it should, since it clearly shouldn't,
  33. especially in this case, 'cause it can't know whether 8+2 or 10 is
  34. wanted.)
  35.  
  36. --
  37. Lars.Wirzenius@helsinki.fi
  38.