Can pre-ANSI C Preprocessor handle symbolic constants in strings?
Roy Johnson
rjohnson at shell.com
Wed Jun 5 01:13:40 AEST 1991
In article <DLBRES14.91May31050315 at pc.usl.edu> dlbres14 at pc.usl.edu (Brumley David M) writes:
> In ANSI C it seems possible to use the Preprocessor to do macro
> substitution of constants in strings:
> #define FIELDSIZE 42
> #define quote(val) #val
> char buffer[FIELDSIZE+1];
> ...
> /* read a field of length FIELDSIZE */
> scanf("%" quote(FIELDSIZE) "s", buffer);
> ...
> So that after the Preprocessor, the 'scanf' call becomes:
> scanf("%42s", buffer);
> Question: How do I do this with a preprocessor that doesn't
> understand the new ANSI '#' operator nor string concatenation? Do I
> have to build the string argument dynamically (allocating an extra
> buffer in the process)?
You might, for this example, want to do
scanf("%*s", FIELDSIZE, buffer);
which is standard/portable. Otherwise you will have to hack
string concatenation until you get an ANSI compiler.
--
=============== !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
More information about the Comp.lang.c
mailing list