Since scanf is the inverse of printf, you could verify this by printing any number with the modifier (just a little tip).*
一般来说,格式之前的数字是“宽度”修饰符。在这种情况下,这意味着您只将一个字节读入一个数字。如果指定%d,则它可以是任意长度的数字。
Example:
#include <stdio.h>
int main() {
int a;
sscanf("1234", "%d", &a);
printf("%d\n", a); // prints 1234
sscanf("1234", "%1d", &a);
printf("%d\n"m a); // prints 1
}
*) this appears to be false for this particular case. Makes sense that numbers are not truncated when specifiying a
%d
format, though, since that would change the meaning of the number. However, for many cases you could try what
printf
would do to predict
scanf
's behavior. But of course, reading the manual or docs on it is always the more helpful approach :)