为什么有一条scanf_s函数不执行
这是一个判断一个数是否是素数的程序:
getchar();
scanf_s("%c",&c); //在这句前加一句;用于接收缓存里的回车符
char s[10];
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.
Note:
The size parameter is of type unsigned, not size_t.
In the case of characters, one may read a single character as follows:
char c;
scanf("%c", &c, 1);
When reading multiple characters for non-null terminated strings, integers are used as the width specification and the buffer size.
char c[4];
scanf("%4c", &c, 4); // not null terminated
For more information, see scanf Width Specification.
TCHAR.H routine
_UNICODE & _MBCS not defined
_MBCS defined
_UNICODE defined
_tscanf_s
scanf_s
scanf_s
wscanf_s
_tscanf_s_l
_scanf_s_l
_scanf_s_l
_wscanf_s_l
For more information, see Format Specification Fields — scanf functions and wscanf Functions.
Requirements
Routine
Required header
scanf_s, _scanf_s_l
<stdio.h>
wscanf_s, _wscanf_s_l
<stdio.h> or <wchar.h>
For additional compatibility information, see Compatibility in the Introduction.
Example
Copy Code
// 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 %d\n", result );
printf( "The contents are: %d %f %c %C %s %S\n", 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 %d\n", result );
wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp,
c, wc, s, ws);
}
Copy Code
71 98.6 h z Byte characters
36 92.3 y n Wide characters
Copy Code
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
.NET Framework Equivalent
System::Console::Read
System::Console::ReadLine
See also Parse methods, such as System::Double::Parse.
See Also
Concepts
Floating-Point Support
Stream I/O
Locale
fscanf, _fscanf_l, fwscanf, _fwscanf_l
printf, _printf_l, wprintf, _wprintf_l
sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
sscanf, _sscanf_l, swscanf, _swscanf_l
Send feedback on this topic to Microsoft.
[解决办法]
Return Value
Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character. If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, scanf_s and wscanf_s return EOF and set errno to EINVAL.
[解决办法]
#include <stdio.h>
char s[]="123 ab 4";
char *p;
int v,n,k;
void main() {
p=s;
while (1) {
k=sscanf(p,"%d%n",&v,&n);
printf("k,v,n=%d,%d,%d\n",k,v,n);
if (1==k) {
p+=n;
} else if (0==k) {
printf("skip char[%c]\n",p[0]);
p++;
} else {//EOF==k
break;
}
}
printf("End.\n");
}
//k,v,n=1,123,3
//k,v,n=0,123,3
//skip char[ ]
//k,v,n=0,123,3
//skip char[a]
//k,v,n=0,123,3
//skip char[b]
//k,v,n=1,4,2
//k,v,n=-1,4,2
//End.
#include<stdio.h>
int main()
{
int isprime(int);
int n,flag=1;
char c;
while(flag)
{
printf("input number:");
while(1 != scanf_s("%d",&n) ) //判断取到多少,一种比较良好的习惯
continue;
if(isprime(n))
printf("True\n");
else printf("Flase\n");
printf("contine or not:\n");
while(getchar() == '\n'); //因为scanf的时候你输入了'\n',所以这里是把缓冲里面的'\n'取出。其实你的程序加了这个就好了
while( 1 != scanf_s("%c",&c) )
continue;
if(c=='n') flag=0;
}
return 0;
}
int isprime(int n)
{
int i,flag;
flag=1;
for(i=2; i<=n/2; i++)
if(n%i==0)
{
flag=0;
break;
}
return(flag);
}