Can pre-ANSI C Preprocessor handle symbolic constants in strings?

Steve Summit scs at adam.mit.edu
Wed Jun 5 06:16:29 AEST 1991


In article <RJOHNSON.91Jun4101340 at olorin.shell.com> rjohnson at shell.com (Roy Johnson) writes:
>In article <DLBRES14.91May31050315 at pc.usl.edu> dlbres14 at 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...).

                                            Steve Summit
                                            scs at adam.mit.edu



More information about the Comp.lang.c mailing list