格式规范字段:scanf 和 wscanf 函数Format Specification Fields: scanf and wscanf Functions

此处的信息适用于整个 scanf 函数系列(包括安全版本),并描述了用于告诉 scanf 函数如何将输入流(如 stdin 的输入流 scanf)分析为插入到程序变量中的值。The information here applies to the entire scanf family of functions, including the secure versions and describes the symbols used to tell the scanf functions how to parse the input stream, such as the input stream stdin for scanf, into values that are inserted into program variables.

格式规范具有以下形式:A format specification has the following form:

%[*] [width] [{h | l | ll | I64 | L}]type%[*] [width] [{h | l | ll | I64 | L}]type

format 参数指定输入的解释,并且可以包含以下一项或多项:The format argument specifies the interpretation of the input and can contain one or more of the following:

  • 空白字符:空白 (' ');制表符 ('\t');或换行符 ('\n')。White-space characters: blank (' '); tab ('\t'); or newline ('\n'). 空白字符将使得 scanf 读取(但不存储)输入中所有连续的空白字符,直至下一个非空白字符。A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non-white-space character. 格式中的一个空白字符与输入中的任何数字(包括 0)和空白字符的组合相匹配。One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.

  • 百分号 (%) 除外的非空白字符。Non-white-space characters, except for the percent sign (%). 非空白字符将使得 scanf 读取(但不存储)匹配的非空白字符。A non-white-space character causes scanf to read, but not store, a matching non-white-space character. 如果输入流中的下一个字符不匹配,则 scanf 将会终止。If the next character in the input stream does not match, scanf terminates.

  • 由百分号 (%) 引入的格式规范。Format specifications, introduced by the percent sign (%). 格式规范将使得 scanf 读取输入中的字符并将其转换为指定类型的值。A format specification causes scanf to read and convert characters in the input into values of a specified type. 该值将分配给参数列表中的一个参数。The value is assigned to an argument in the argument list.

    格式为从左向右读取。The format is read from left to right. 不符合格式规范的字符应匹配输入流中的字符序列;将扫描但不存储输入流中的匹配字符。Characters outside format specifications are expected to match the sequence of characters in the input stream; the matching characters in the input stream are scanned but not stored. 如果输入流中的字符与格式规范冲突,则 scanf 将终止,并且将该字符留在输入流中,就像没有读取过它一样。If a character in the input stream conflicts with the format specification, scanf terminates, and the character is left in the input stream as if it had not been read.

    遇到第一个格式规范时,第一个输入字段的值将根据此规范进行转换并储存在第一个 argument 指定的位置中。When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. 第二个格式规范将使得第二个输入字段进行转换并储存在第二个 argument 中,依此类推,直至格式字符串的末尾。The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.

    将输入字段定义为第一个空白字符(空格、制表符或换行符)之前的所有字符,或第一个无法根据格式规范转换的字符之前的所有字符,或在达到最大字段宽度(如果已指定)之前的所有字符。An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. 如果给定的规范有太多参数,则将计算但忽略多余的参数。If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. 如果格式规范没有足够参数,则结果不可预知。The results are unpredictable if there are not enough arguments for the format specification.

    格式规范的每个字段是一个用于指定特定格式选项的字符或数字。Each field of the format specification is a single character or a number signifying a particular format option. 位于最后一个可选格式字段之后的 type 字符决定将输入字段解释为字符、字符串还是数字。The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.

    最简单的格式规范仅包含百分号和一个 type 字符(例如,%s)。The simplest format specification contains only the percent sign and a type character (for example, %s). 如果百分号 (%) 后跟一个没有意义的字符作为格式控制字符,则该字符及其后面的字符(直至下一个百分号)将被视为普通字符序列,即必须与输入匹配的字符序列。If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. 例如,若要指定要输入的百分号字符,请使用 %%For example, to specify that a percent-sign character is to be input, use %%.

    百分号后面的星号 (*) 将取消下一个输入字段的分配(将被解释为指定类型的字段)。An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. 将扫描但不储存该字段。The field is scanned but not stored.

    _s 函数系列的安全版本(具有 scanf 后缀的版本)需要在每个 cCsS[ 类型的参数之后立即传入一个缓冲区大小参数。The secure versions (those with the _s suffix) of the scanf family of functions require that a buffer size parameter be passed immediately following each parameter of type c, C, s, S or [. 有关 scanf 系列函数的安全版本的更多信息,请参阅 scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lFor more information on the secure versions of the scanf family of functions, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l.

另请参阅See Also

scanf 宽度规范 scanf Width Specification
scanf 类型字段字符 scanf Type Field Characters
scanf、_scanf_l、wscanf、_wscanf_l scanf, _scanf_l, wscanf, _wscanf_l
scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lscanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l