scanf_s,在ANSIC中是没有的,只有scanf(),scanf()在读取时不检查边界,所以可能会造成内存泄露。
ANSI C中没有scanf_s(),只有scanf(),scanf()在读取时不检查边界,所以可能会造成内存泄露。所以vc++2005/2008中提供了scanf_s(),在调用时,必须提供一个数字以表明最多读取多少位字符。MSDN中例子:
// crt_scanf_s.c
// This program uses the scanf_s and wscanf_s 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_s( "%d %f %c %C %s %S", &i, &fp, &c, 1, &wc, 1, s, 80, ws, 80 );
printf( "The number of fields input is %dn", result );
printf( "Thecontentsare: %d %f %c %C %s %Sn", i, fp, c, wc, s, ws);
result = wscanf_s( L"%d %f %hc %lc %S %ls", &i, &fp, &c, 2, &wc, 1, s, 80, ws, 80 );
wprintf( L"The number of fields input is %dn", result );
wprintf( L"The contents are: %d %f %C %c %hs %sn", i, fp, c, wc, s, ws);
return 0;
}