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.


int scanf(
   const char *format [,
int _scanf_l(
   const char *format,
   locale_t locale [,
int wscanf(
   const wchar_t *format [,
int _wscanf_l(
   const wchar_t *format,
   locale_t locale [,


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

可选参数。Optional arguments.

要使用的区域设置。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.

如果格式NULL调用指针,无效参数处理程序,如中所述参数验证If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,这些函数将返回EOF并设置errnoEINVALIf 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.


Scanf函数从标准输入流中读取数据stdin并将数据写入到指定的位置参数The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument. 每个自变量必须是指向类型中的类型说明符相对应的变量的指针格式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.

wscanf是宽字符版本scanf;格式参数wscanf是宽字符字符串。wscanf is a wide-character version of scanf; the format argument to wscanf is a wide-character string. wscanfscanf如果在 ANSI 模式下打开流,则行为相同。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_tscanf scanfscanf scanfscanf wscanfwscanf
_tscanf_l_tscanf_l _scanf_l_scanf_l _scanf_l_scanf_l _wscanf_l_wscanf_l

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


例程所返回的值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.


// 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 characters
The 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/OStream I/O
fscanf、_fscanf_l、fwscanf、_fwscanf_lfscanf, _fscanf_l, fwscanf, _fwscanf_l
printf、_printf_l、wprintf、_wprintf_lprintf, _printf_l, wprintf, _wprintf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
sscanf、_sscanf_l、swscanf、_swscanf_lsscanf, _sscanf_l, swscanf, _swscanf_l