scanf 宽度规范scanf Width Specification

此信息适用于解释 scanf 系列函数中的格式字符串,包括 scanf_s 等安全版本。This information applies to the interpretation of format strings in the scanf family of functions, including the secure versions such as scanf_s. 这些函数通常假定将输入流划分为标记序列。These functions normally assume the input stream is divided into a sequence of tokens. 标记通过空白字符(空格、制表符或换行)进行分隔,或者在数值类型的情况下,按无法转换为数值文本的第一个字符所示由数值数据类型的自然末尾进行分隔。Tokens are separated by whitespace (space, tab, or newline), or in the case of numerical types, by the natural end of a numerical data type as indicated by the first character that cannot be converted into numerical text. 然而,宽度规范可能用于在标记的自然末尾之前停止输入分析。However, the width specification may be used to cause parsing of the input to stop before the natural end of a token.

宽度规范由 % 和类型字段说明符之间的字符组成,其中可能包括名为宽度字段的正整数以及一个或多个表示字段大小的字符,这些字符还可能被视为字段类型的修饰符,例如指示整数类型是 short 还是 longThe width specification consists of characters between the % and the type field specifier, which may include a positive integer called the width field and one or more characters indicating the size of the field, which may also be considered as modifiers of the type of the field, such as an indication of whether the integer type is short or long. 此类字符称为大小前缀。Such characters are referred to as the size prefix.

宽度字段The Width Field

宽度字段是十进制正整数,用于控制为该字段读取的字符最大数量。The width field is a positive decimal integer controlling the maximum number of characters to be read for that field. 不超过宽度的字符会转换并存储在相应的 argument 中。No more than width characters are converted and stored at the corresponding argument. 如果达到宽度之前出现空白字符(空格、制表符或换行)或根据给定格式不能转换的字符,则可能读取少于宽度的字符。Fewer than width characters may be read if a whitespace character (space, tab, or newline) or a character that cannot be converted according to the given format occurs before width is reached.

宽度规范是单独的,且不同于这些函数的安全版本(即 scanf_swscanf_s 等)所需的缓冲区大小参数。The width specification is separate and distinct from the buffer size argument required by the secure versions of these functions (i.e., scanf_s, wscanf_s, etc.). 在以下示例中,宽度规范为 20,这表示从输入流中最多可读取 20 个字符。In the following example, the width specification is 20, indicating that up to 20 characters are to be read from the input stream. 缓冲区长度为 21,其中包括用于可能 20 个字符以及 null 终止符的空间:The buffer length is 21, which includes room for the possible 20 characters plus the null terminator:

char str[21];  
scanf_s("%20s", str, 21);  

如果未使用宽度字段,则 scanf_s 会尝试将整个标记读取到字符串中。If the width field is not used, scanf_s will attempt to read the entire token into the string. 如果指定的大小不足以容纳整个标记,则不会向目标字符串写入任何内容。If the size specified is not large enough to hold the entire token, nothing will be written to the destination string. 如果指定了宽度字段,则将标记中的第一个宽度字符与 null 终止符一起写入目标字符串。If the width field is specified, then the first width characters in the token will be written to the destination string along with the null terminator.

大小前缀The Size Prefix

可选前缀 hlllI64L 指示 argument 的大小(long 或 short、单字节字符或宽字符,具体取决于它们修饰的类型字符)。The optional prefixes h, l, ll, I64, and L indicate the size of the argument (long or short, single-byte character or wide character, depending upon the type character that they modify). 这些格式规范字符与类型字符一起用于 scanfwscanf 函数中,用以指定参数的解释,如下表所示。These format-specification characters are used with type characters in scanf or wscanf functions to specify interpretation of arguments as shown in the following table. 类型前缀 I64 是 Microsoft 扩展,与 ANSI 不兼容。The type prefix I64 is a Microsoft extension and is not ANSI compatible. 有关类型字符及其含义的描述,请参阅 scanf 类型字段字符中的“scanf 函数的类型字符”表。The type characters and their meanings are described in the "Type Characters for scanf functions" table in scanf Type Field Characters.

备注

与类型为 char 的数据一起使用时,hlL 前缀是 Microsoft 扩展。The h, l, and L prefixes are Microsoft extensions when used with data of type char.

scanf 和 wscanf 格式类型说明符的大小前缀Size Prefixes for scanf and wscanf Format-Type Specifiers

若要指定To specify 使用前缀Use prefix 及类型说明符With type specifier
双精度double ll eEfgGe, E, f, g, or G
long double(与 double 相同)long double (same as double) LL eEfgGe, E, f, g, or G
long intlong int ll dioxXd, i, o, x, or X
long unsigned intlong unsigned int ll uu
long longlong long llll dioxXd, i, o, x, or X
short int hh dioxXd, i, o, x, or X
short unsigned intshort unsigned int hh uu
int64int64 I64I64 diouxXd, i, o, u, x, or X
scanf 的单字节字符Single-byte character with scanf hh cCc or C
wscanf 的单字节字符Single-byte character with wscanf hh cCc or C
scanf 的宽字符Wide character with scanf ll cCc or C
wscanf 的宽字符Wide character with wscanf ll cCc, or C
scanf 的单字节字符串Single-byte - character string with scanf hh sSs or S
wscanf 的单字节字符串Single-byte - character string with wscanf hh sSs or S
scanf 的宽字符字符串Wide-character string with scanf ll sSs or S
wscanf 的宽字符字符串Wide-character string with wscanf ll sSs or S

以下是 hlscanf_s 函数和 wscanf_s 函数结合使用的示例:The following examples use h and l with scanf_s functions and wscanf_s functions:

scanf_s("%ls", &x, 2);     // Read a wide-character string  
wscanf_s(L"%hC", &x, 2);    // Read a single-byte character  

如果在 scanf 系列中使用了不安全的函数,则省略表示先前参数的缓冲区长度的大小参数。If using an unsecure function in the scanf family, omit the size parameter indicating the buffer length of the preceding argument.

读取未分隔的字符串Reading Undelimited strings

若要读取未由空白字符分隔的字符串,可将括号 ([ ]) 中的字符集替换为 s(字符串)类型字符。To read strings not delimited by whitespace characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. 括号中的字符集称为控制字符串。The set of characters in brackets is referred to as a control string. 相应的输入字段一直读取到控制字符串中未显示的第一个字符。The corresponding input field is read up to the first character that does not appear in the control string. 如果字符集中的第一个字符是插入符号 (^),则效果相反:输入字段一直读取到字符集其余部分中显示的第一个字符。If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.

请注意,%[a-z]%[z-a] 解释为与 %[abcde...z] 等同。Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. 这是一个常见的 scanf 函数扩展,但请注意,ANSI 标准并不需要此扩展。This is a common scanf function extension, but note that the ANSI standard does not require it.

读取未终止的字符串Reading Unterminated strings

若要在不保存终止空字符 ('\0') 的情况下保存字符串,请使用规范 %nc,其中 n 是十进制整数。To store a string without storing a terminating null character ('\0'), use the specification %nc where n is a decimal integer. 在这种情况下,c 类型字符表示自变量指向字符数组。In this case, the c type character indicates that the argument is a pointer to a character array. 接下来的 n 个字符从输入流读取到指定位置,不附加任何空字符 ('\0')。The next n characters are read from the input stream into the specified location, and no null character ('\0') is appended. 如果 n 未指定,则其默认值为 1。If n is not specified, its default value is 1.

当 scanf 停止读取字段时When scanf stops reading a field

scanf 函数逐个字符地扫描每个输入字段。The scanf function scans each input field, character by character. 由于各种原因,可能在达到空格字符之前停止读取特定输入字段:It may stop reading a particular input field before it reaches a space character for a variety of reasons:

  • 已达到指定宽度。The specified width has been reached.

  • 下一个字符无法按指定进行转换。The next character cannot be converted as specified.

  • 下一个字符与应该匹配的控制字符串中的字符冲突。The next character conflicts with a character in the control string that it is supposed to match.

  • 下一个字符没有在给定字符集中出现。The next character fails to appear in a given character set.

    无论什么原因,当 scanf 函数停止读取输入字段时,下一个输入字段都被视为从第一个未读取的字符处开始。For whatever reason, when the scanf function stops reading an input field, the next input field is considered to begin at the first unread character. 冲突字符(如果有)被视为未读取,且是下一个输入字段的第一个字符或输入流上后续读取操作中的第一个字符。The conflicting character, if there is one, is considered unread and is the first character of the next input field or the first character in subsequent read operations on the input stream.

另请参阅See Also

scanf、_scanf_l、wscanf、_wscanf_l scanf, _scanf_l, wscanf, _wscanf_l
scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_l scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
格式规范字段:scanf 和 wscanf 函数 Format Specification Fields: scanf and wscanf Functions
scanf 类型字段字符scanf Type Field Characters