How can I use a variable to specify the max number of chars scanf()
should read in?
For example using printf()
you can use the * like so
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"
Kernighan and Pike recommend using snprintf() to create the format string. I developed this idea into a method that safely reads strings:
void scan_string(char * buffer, unsigned length) {
char format[12]; // Support max int value for the format %<num>s
snprintf(format, sizeof(format), "%%%ds", length - 1); // Generate format
scanf(format, buffer);
}
int main() {
char string[5];
scan_string(string, sizeof(string));
printf("%s\n", string);
}
You can't. You need to use something other than scanf()
. A good and popular choice is fgets()
, although its semantics are slightly different: fgets()
will read a line of input, whereas scanf()
with %s
will read whitespace separated sequences of characters.
To use fgets()
, you'd want something like:
fgets(string, MAXVAL, stdin);
If for some reason you really want to use scanf()
, have a look at this question: How to prevent scanf causing a buffer overflow in C?
#include <stdio.h>
#define MAXLEN 5
#define S_(x) #x
#define S(x) S_(x)
int main(void){
char string[MAXLEN+1];
scanf("%" S(MAXLEN) "s", string);
printf("<%.*s>\n", MAXLEN, string);
return 0;
}