This is one of the most annoying discrepancies between the *printf() and *scanf() families. For some reason, you can use wildcards in *printf() conversion specifiers and provide values ββfor them in the argument list, but there is no equivalent for *scanf() .
Be that as it may, I would prefer to use a separate sprintf() operation to build a format string, rather than relying on macros as a string:
#define LENGTH 20 char word[LENGTH+1]; char format[5]; sprintf(format, "%%%ds", LENGTH); fscanf(file, format, word);
At least this is somewhat intuitive; as Thorak points out, you need to go through two levels of indirection using string macros (since you donβt want to screw the macro, but that extends the macro).
source share