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

读取标准输入流中的格式化数据。Reads formatted data from the standard input stream. 提供这些函数的更多安全版本;请参阅 scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lMore secure versions of these function are available; see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l.

语法Syntax

int scanf(  
   const char *format [,  
   argument]...   
);  
int _scanf_l(  
   const char *format,  
   locale_t locale [,  
   argument]...   
);  
int wscanf(  
   const wchar_t *format [,  
   argument]...   
);  
int _wscanf_l(  
   const wchar_t *format,  
   locale_t locale [,  
   argument]...   
);  

参数Parameters

format
格式控制字符串。Format control string.

argument
可选参数。Optional arguments.

locale
要使用的区域设置。The locale to use.

返回值Return Value

返回已成功转换和分配的字段数量;返回值不包括已读取但未分配的字段。Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. 返回值为 0 表示没有分配任何字段。A return value of 0 indicates that no fields were assigned.

如果 formatNULL 指针,则调用无效参数处理程序,如参数验证中所述。If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回 EOF 并将 errno 设置为 EINVALIf execution is allowed to continue, these functions return EOF and set errno to EINVAL.

有关这些代码及其他错误代码的信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrFor information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

备注Remarks

scanf 函数从标准输入流 stdin 中读取数据,并将数据写入到 argument 指定的位置。The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument. 每个 argument 必须为指向类型的变量的指针,该类型与 format 中的类型说明符对应。Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. 如果复制出现在重叠的字符串之间,则该行为不确定。If copying takes place between strings that overlap, the behavior is undefined.

重要

使用 scanf 读取字符串时,请始终指定 %s 格式的宽度(例如 "%32s" 而不是 "%s");否则,输入格式不正确很容易导致缓冲区溢出。When reading a string with scanf, always specify a width for the %s format (for example, "%32s" instead of "%s"); otherwise, improperly formatted input can easily cause a buffer overrun. 或者,可以考虑使用 scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lfgetsAlternately, consider using scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l or fgets.

wscanfscanf 的宽字符版本;formatwscanf 参数是宽字符字符串。wscanf is a wide-character version of scanf; the format argument to wscanf is a wide-character string. 如果在 ANSI 模式下打开流,则 wscanfscanf 的行为相同。wscanf and scanf behave identically if the stream is opened in ANSI mode. scanf 当前不支持 UNICODE 流的输入。scanf doesn't currently support input from a UNICODE stream.

这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前线程区域设置。The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tscanf scanf scanf wscanf
_tscanf_l _scanf_l _scanf_l _wscanf_l

有关详细信息,请参阅格式规范字段 — scanf 函数和 wscanf 函数For more information, see Format Specification Fields — scanf functions and wscanf Functions.

惠?Requirements

例程所返回的值Routine 必需的标头Required header
scanf, _scanf_lscanf, _scanf_l <stdio.h><stdio.h>
wscanf, _wscanf_lwscanf, _wscanf_l <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 控制台中,与关联的标准流句柄stdinstdout,和stderr,必须将重定向,然后 C 运行时函数可以在 UWP 应用中使用它们。The standard stream handles that are associated with the console, stdin, stdout, and stderr, must be redirected before C run-time functions can use them in UWP apps. 有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_scanf.c  
// compile with: /W3  
 /* This program uses the scanf and wscanf functions  
  * to read formatted input.  
  */  

#include <stdio.h>  

int main( void )  
{  
   int   i, result;  
   float fp;  
   char  c, s[81];  
   wchar_t wc, ws[81];  
   result = scanf( "%d %f %c %C %80s %80S", &i, &fp, &c, &wc, s, ws ); // C4996  
   // Note: scanf and wscanf are deprecated; consider using scanf_s and wscanf_s  
   printf( "The number of fields input is %d\n", result );  
   printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws);  
   result = wscanf( L"%d %f %hc %lc %80S %80ls", &i, &fp, &c, &wc, s, ws ); // C4996  
   wprintf( L"The number of fields input is %d\n", result );  
   wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp, c, wc, s, ws);  
}  

      71 98.6 h z Byte characters  
36 92.3 y n Wide charactersThe number of fields input is 6  
The contents are: 71 98.599998 h z Byte characters  
The number of fields input is 6  
The contents are: 36 92.300003 y n Wide characters  

请参阅See Also

浮点支持 Floating-Point Support
流 I/O Stream I/O
区域设置 Locale
fscanf、_fscanf_l、fwscanf、_fwscanf_l fscanf, _fscanf_l, fwscanf, _fwscanf_l
printf、_printf_l、wprintf、_wprintf_l printf, _printf_l, wprintf, _wprintf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
sscanf、_sscanf_l、swscanf、_swscanf_lsscanf, _sscanf_l, swscanf, _swscanf_l