Newsgroups: comp.lang.c
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!nuchat!shell!shell!rjohnson
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: Can pre-ANSI C Preprocessor handle symbolic constants in strings?
In-Reply-To: scs@adam.mit.edu's message of Tue, 4 Jun 91 20:16:29 GMT
Message-ID: <RJOHNSON.91Jun10114252@olorin.shell.com>
Sender: usenet@shell.shell.com (USENET News System)
Organization: Shell Development Company, Bellaire Research Center, Houston, TX
References: <DLBRES14.91May31050315@pc.usl.edu>
	<RJOHNSON.91Jun4101340@olorin.shell.com>
	<1991Jun4.201629.29013@athena.mit.edu>
Date: 10 Jun 91 11:42:52


In article <1991Jun4.201629.29013@athena.mit.edu> scs@adam.mit.edu (Steve Summit) writes:
> In article <RJOHNSON.91Jun4101340@olorin.shell.com> rjohnson@shell.com (Roy Johnson) writes:
> >In article <DLBRES14.91May31050315@pc.usl.edu> dlbres14@pc.usl.edu (Brumley David M) writes:
> >> /* read a field of length FIELDSIZE */
> >> scanf("%" quote(FIELDSIZE) "s", buffer); 
> >> So that after the Preprocessor, the 'scanf' call becomes:
> >> scanf("%42s", buffer);

> >You might, for this example, want to do
> >scanf("%*s", FIELDSIZE, buffer);
> >which is standard/portable.

> I won't say "BZZT!", because I had halfway composed an article
> making the same suggestion, until I noticed that Mr. Brumley had
> asked about *scanf*, not printf.  There's this thoroughly
> annoying asymmetry between printf and scanf: for scanf, the '*'
> format-specifier flag indicates assignment suppression, *not*
> that the field width should be picked up from the argument list.
> Building a format string (either at compile time with string
> concatenation if you can hack it, or at run time with strcat
> and/or sprintf) is really what you have to do to get "variable"
> scanf field widths (unless you have an 8-10th edition scanf...).

You're right; I bone-headed it (cancelled my other posting
after seeing your correction, too).

In compensation, I illustrate your suggestion:

char fmt[10];

/* The first two %'s make one % in the string */
sprintf(fmt, "%%%ds", FIELDSIZE);
scanf(fmt, buffer);

which is, dare I say, standard, portable, and correct?
--
=============== !You!can't!get!here!from!there!rjohnson ===============
Feel free to correct me, but don't preface your correction with "BZZT!"
Roy Johnson, Shell Development Company
