请各位高手解答scanf_s(“%S”, ws, 10)和scanf_s(“%S”, ws);的区别，原来一直以为两个没什么区别，但是今天写书上的习题的时候用scanf_s(“%S”, ws)则程序会出故障，然后说让调试程序。如果用scanf_s(“%S”, ws, 10)代替则程序正常完成。请高手解答一下！
scanf_s()是vs下对scanf()的改进，scanf_s(“%s”, ws, 10);的意思是读入字符串到地址ws，最多读入10个字符
ws = *(ws+10);
你有吗? 你有没有? 你没有吧?
Collapse AllExpand All Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript
Run-Time Library Reference
scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
Example See Also Send Feedback
Read formatted data from the standard input stream. These are versions of scanf, _scanf_l, wscanf, _wscanf_l with security enhancements as described in Security Enhancements in the CRT.
For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.
wscanf_s is a wide-character version of scanf_s; the format argument to wscanf_s is a wide-character string. wscanf_s and scanf_s behave identically identically if the stream is opened in ANSI mode. scanf_s doesn””t currently support input from a UNICODE stream.
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.
Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if reading a string, the buffer size for that string is passed as follows:
scanf(“%9s”, s, 10);
The buffer size includes the terminating null. A width specification field may be used to ensure that the token read in will fit into the buffer. If no width specification field is used, and the token read is too big to fit in the buffer, nothing will be written to that buffer.
In the case of characters, one may read a single character as follows:
scanf(“%c”, &c, 1);
When reading multiple characters for non-null terminated strings, integers are used as the width specification and the buffer size.
scanf(“%4c”, &c, 4); // not null terminated
For more information, see scanf Width Specification.
For more information, see Format Specification Fields — scanf functions and wscanf Functions.
For additional compatibility information, see Compatibility in the Introduction.
int main( void )
result = scanf_s( “%d %f %c %C %s %S”, &i, &fp, &c, 1,
.NET Framework Equivalent
See also Parse methods, such as System::Double::Parse.