home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18949 < prev    next >
Encoding:
Internet Message Format  |  1992-12-29  |  1.4 KB

  1. Path: sparky!uunet!cs.utexas.edu!sun-barr!rutgers!news.columbia.edu!cunixa.cc.columbia.edu!ta-psc10
  2. From: ta-psc10@cunixa.cc.columbia.edu (Po Shan Cheah)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Scanf field width macro?
  5. Message-ID: <1992Dec28.223305.15749@news.columbia.edu>
  6. Date: 28 Dec 92 22:33:05 GMT
  7. References: <1992Dec28.213129.8983@nosc.mil>
  8. Sender: usenet@news.columbia.edu (The Network News)
  9. Reply-To: ta-psc10@cunixa.cc.columbia.edu (Po Shan Cheah)
  10. Followup-To: poster
  11. Organization: Columbia University
  12. Lines: 29
  13. Nntp-Posting-Host: cunixa.cc.columbia.edu
  14.  
  15. In article <1992Dec28.213129.8983@nosc.mil> mitch@nosc.mil (Ray Mitchell) writes:
  16. >
  17. >Consider the statements
  18. >
  19. >   printf("%*s", field_width, char_buffer);
  20. >    scanf("%5s", char_buffer);
  21. >
  22. >I would like to avoid embedding the magic field width constant in the
  23. >scanf.  I don't need it to be a variable field width like in printf
  24. >but only need it to be in terms of some constant width macro I've
  25. >previously defined.  I've tried various things using stringization,
  26. >token pasting, etc. to no avail.  A solution which does not depend
  27. >on some non-standard preprocessor/compiler behavior is required.
  28.  
  29. Try this:
  30.  
  31. #define FIELD_WIDTH 5
  32.  
  33. #define tostr2(x) #x
  34. #define tostr(x) tostr2(x)
  35.  
  36. scanf("%" tostr(FIELD_WIDTH) "s", char_buffer);
  37.  
  38. Hope this works.
  39.  
  40. Po Shan Cheah
  41. ta-psc10@columbia.edu
  42. pc30@columbia.edu
  43. pcheah@nyx.cs.du.edu
  44.