scanf 类型字段字符scanf Type Field Characters

以下信息适用于函数的所有 scanf 系列,包括 scanf_s等安全版本。The following information applies to any of the scanf family of functions, including the secure versions, such as scanf_s.

type 字符是唯一必需的格式字段;它显示在任何可选格式字段的后面。The type character is the only required format field; it appears after any optional format fields. type 字符决定了关联的自变量是解释为字符、字符串还是数字。The type character determines whether the associated argument is interpreted as a character, string, or number.

scanf 函数的类型字符Type Characters for scanf functions

字符Character 预期输入类型Type of input expected 参数类型Type of argument 安全版本中的大小参数?Size argument in secure version?
c 字符。Character. scanf 函数结合使用时,指定单字节字符;与 wscanf 函数结合使用时,指定宽字符。When used with scanf functions, specifies single-byte character; when used with wscanf functions, specifies wide character. 指定了 c 时,也会读取通常被跳过的空白字符。White-space characters that are ordinarily skipped are read when c is specified. 若要读取下一个非空白单字节字符,请使用 %1s;若要读取下一个非空白宽字符,请使用 %1wsTo read next non-white-space single-byte character, use %1s; to read next non-white-space wide character, use %1ws. char 函数结合使用时,指针指向 scanf ;与 wchar_t 函数结合使用时,指针指向 wscanfPointer to char when used with scanf functions, pointer to wchar_t when used with wscanf functions. 必需。Required. 大小中不包括 null 终止符的占用空间。Size does not include space for a null terminator.
C 相反大小字符。Opposite size character. scanf 函数结合使用时,指定宽字符;与 wscanf 函数结合使用时,指定单字节字符。When used with scanf functions, specifies wide character; when used with wscanf functions, specifies single-byte character. 指定了 C 时,也会读取通常被跳过的空白字符。White-space characters that are ordinarily skipped are read when C is specified. 若要读取下一个非空白单字节字符,请使用 %1s;若要读取下一个非空白宽字符,请使用 %1wsTo read next non-white-space single-byte character, use %1s; to read next non-white-space wide character, use %1ws. wchar_t 函数结合使用时,指针指向 scanf ;与 char 函数结合使用时,指针指向 wscanfPointer to wchar_t when used with scanf functions, pointer to char when used with wscanf functions. 必需。Required. 大小参数不包括 null 终止符的占用空间。Size argument does not include space for a null terminator.
d 十进制整数。Decimal integer. 指向 int的指针。Pointer to int. 不是。No.
i 一个整数。An integer. 如果输入字符串以“0x”或“0X”开始,则为十六进制;如果字符串以“0”开始,则为八进制;其余情况为十进制。Hexadecimal if the input string begins with "0x" or "0X", octal if the string begins with "0", otherwise decimal. 指向 int的指针。Pointer to int. 不是。No.
o 八进制整数。Octal integer. 指向 int的指针。Pointer to int. 不是。No.
p 十六进制数字中的指针地址。A pointer address in hexadecimal digits. 最大位数读数取决于指针大小(32 或 64 位),指针大小取决于计算机体系结构。The maximum number of digits read depends on the size of a pointer (32 or 64 bits), which depends on the machine architecture. 接受“0x”或“0X”作为前缀。"0x" or "0X" are accepted as prefixes. 指向 void*的指针。Pointer to void*. 不是。No.
u 无符号十进制整数。Unsigned decimal integer. 指向 unsigned int的指针。Pointer to unsigned int. 不是。No.
x 十六进制整数。Hexadecimal integer. 指向 int的指针。Pointer to int. 不是。No.
e, E, f, F, g, Ge, E, f, F, g, G 包含可选符号(+ 或 -)的浮点值、包含小数点的一个或多个十进制数字系列,以及后跟可选带符号整数值的可选指数(“e”或“E”)。Floating-point value consisting of optional sign (+ or -), series of one or more decimal digits containing decimal point, and optional exponent ("e" or "E") followed by an optionally signed integer value. 指向 float的指针。Pointer to float. 不是。No.
a, Aa, A 由一系列一个或多个包含可选小数点的十六进制数字和后跟十进制值的一个指数(“p”或“P”)所组成的浮点值。Floating-point value consisting of a series of one or more hexadecimal digits containing an optional decimal point, and an exponent ("p" or "P") followed by a decimal value. 指向 float的指针。Pointer to float. 不是。No.
n 未从流或缓冲区读取任何输入。No input read from stream or buffer. 指向 int的指针,其中存储了当前调用 scanf 函数或 wscanf 函数时从流或缓冲区到该点成功读取的字符数。Pointer to int, into which is stored number of characters successfully read from stream or buffer up to that point in current call to scanf functions or wscanf functions. 不是。No.
s 字符串,直到第一个空白字符(空格、制表符或换行)。String, up to first white-space character (space, tab or newline). 若要读取未被空格字符分隔的字符串,请使用一组方括号 ([ ]),如 scanf Width Specification等安全版本。To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf Width Specification. scanf 函数结合使用时,表示单字节字符数组;与 wscanf 函数结合使用时,表示宽字符数组。When used with scanf functions, signifies single-byte character array; when used with wscanf functions, signifies wide-character array. 无论哪种情况,字符数组都必须具有足够大小以容纳输入字段和自动附加的终止 null 字符。In either case, character array must be large enough for input field plus terminating null character, which is automatically appended. 必需。Required. 大小中包括 null 终止符的占用空间。Size includes space for a null terminator.
S 相反大小字符字符串,直到第一个空白字符(空格、制表符或换行)。Opposite-size character string, up to first white-space character (space, tab or newline). 若要读取未被空格字符分隔的字符串,请使用一组方括号 ([ ]),如 scanf 宽度规范中所述。To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf Width Specification. scanf 函数结合使用时,表示宽字符数组;与 wscanf 函数结合使用时,表示单字节字符数组。When used with scanf functions, signifies wide-character array; when used with wscanf functions, signifies single-byte-character array. 无论哪种情况,字符数组都必须具有足够大小以容纳输入字段和自动附加的终止 null 字符。In either case, character array must be large enough for input field plus terminating null character, which is automatically appended. 必需。Required. 大小中包括 null 终止符的占用空间。Size includes space for a null terminator.

大小参数(如需要)应紧跟在其适用于的参数之后传递到参数列表中。The size arguments, if required, should be passed in the parameter list immediately following the argument they apply to. 例如,以下代码:For example, the following code:

char string1[11], string2[9];  
scanf_s("%10s %8s", string1, 11, string2, 9);  

将最大长度为 10 的字符串读入 string1,并将最大长度为 8 的字符串读入 string2reads a string with a maximum length of 10 into string1, and a string with a maximum length of 8 into string2. 缓冲区大小应至少比宽度规范多一个,因为必须为 null 终止符保留空间。The buffer sizes should be at least one more than the width specifications since space must be reserved for the null terminator.

无论是使用函数的单字节字符版本还是宽字符版本,格式字符串都可以处理单字节或宽字符输入。The format string can handle single-byte or wide character input regardless of whether the single-byte character or wide-character version of the function is used. 因此,若要读取包含 scanf 函数和 wscanf 函数的单字节字符或宽字符,请按如下所述使用格式说明符。Thus, to read single-byte or wide characters with scanf functions and wscanf functions, use format specifiers as follows.

若要将字符读取为To read character as 使用此函数Use this function 使用这些格式说明符With these format specifiers
单字节single byte scanf 函数scanf functions chchCc, hc, or hC
单字节single byte wscanf 函数wscanf functions ChchCC, hc, or hC
wide wscanf 函数wscanf functions clclCc, lc, or lC
wide scanf 函数scanf functions ClclCC, lc, or lC

若要扫描包含 scanf 函数和 wscanf 函数的字符串,请将上表与格式类型说明符 sS 结合使用,而不是与 cC一起使用。To scan strings with scanf functions, and wscanf functions, use the above table with format type-specifiers s and S instead of c and C.

另请参阅See Also

scanf、_scanf_l、wscanf、_wscanf_lscanf, _scanf_l, wscanf, _wscanf_l