如何使用变量指定scanf()应读入的最大字符数?
例如使用printf()你可以像这样使用*
#define MAXVAL 5
printf("Print at maximum MAXVAL chars: %.*s\n", MAXVAL, "myStringHere");
Run Code Online (Sandbox Code Playgroud)
这只会打印5个字符,如何才能scanf在MAXVAL中读取?MAXVAL必须用作长度说明符.我不能简单地做
scanf("%5s", string);
Run Code Online (Sandbox Code Playgroud)
现在我只能想到读入一个大数组scanf然后ssprintf用来将字符串存储到我的长度有限的字符串中.然而,使用长度说明符会更容易.