scanf() variable length specifier

You can use the C preprocessor to help you with that.

#define STR2(x) #x
#define STR(X) STR2(X)
scanf("%" STR(MAXVAL) "s", string);

The processor combines "%" STR(MAXVAL) "s" to "%5s"

Leave a Comment