Newsgroups: comp.lang.c Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!mintaka!bloom-picayune.mit.edu!news From: scs@adam.mit.edu (Steve Summit) Subject: Re: Can pre-ANSI C Preprocessor handle symbolic constants in strings? Message-ID: <1991Jun4.201629.29013@athena.mit.edu> Summary: scanf("%*...") isn't like printf("%*...") Sender: news@athena.mit.edu (News system) Reply-To: scs@adam.mit.edu Organization: Thermal Technologies, Cambridge, MA References: Date: Tue, 4 Jun 91 20:16:29 GMT Lines: 24 In article rjohnson@shell.com (Roy Johnson) writes: >In article 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...). Steve Summit scs@adam.mit.edu