home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!sun-barr!rutgers!news.columbia.edu!cunixa.cc.columbia.edu!ta-psc10
- From: ta-psc10@cunixa.cc.columbia.edu (Po Shan Cheah)
- Newsgroups: comp.lang.c
- Subject: Re: Scanf field width macro?
- Message-ID: <1992Dec28.223305.15749@news.columbia.edu>
- Date: 28 Dec 92 22:33:05 GMT
- References: <1992Dec28.213129.8983@nosc.mil>
- Sender: usenet@news.columbia.edu (The Network News)
- Reply-To: ta-psc10@cunixa.cc.columbia.edu (Po Shan Cheah)
- Followup-To: poster
- Organization: Columbia University
- Lines: 29
- Nntp-Posting-Host: cunixa.cc.columbia.edu
-
- In article <1992Dec28.213129.8983@nosc.mil> mitch@nosc.mil (Ray Mitchell) writes:
- >
- >Consider the statements
- >
- > printf("%*s", field_width, char_buffer);
- > scanf("%5s", char_buffer);
- >
- >I would like to avoid embedding the magic field width constant in the
- >scanf. I don't need it to be a variable field width like in printf
- >but only need it to be in terms of some constant width macro I've
- >previously defined. I've tried various things using stringization,
- >token pasting, etc. to no avail. A solution which does not depend
- >on some non-standard preprocessor/compiler behavior is required.
-
- Try this:
-
- #define FIELD_WIDTH 5
-
- #define tostr2(x) #x
- #define tostr(x) tostr2(x)
-
- scanf("%" tostr(FIELD_WIDTH) "s", char_buffer);
-
- Hope this works.
-
- Po Shan Cheah
- ta-psc10@columbia.edu
- pc30@columbia.edu
- pcheah@nyx.cs.du.edu
-