当我输入a
时,输出是not a
。条件为真,为什么输出是not a
?当我使用getchar
而不是scanf_s
,它工作正常。有什么问题?
char op;
scanf_s("%c", &op);
if ( op == 'a' )
printf("the character is a");
else
printf("not a");
尝试scanf()
代替scanf_s()
.
说明符%c
(另外两个这样的例外%s
,%[
)需要第三个参数大小-
scanf_s("%c", &op, 1); // 1 to read single character
第三个参数应该是sizeof
类型。仅当由实现定义并且用户在 include之前定义为整数常量scanf_s
时才保证可用。__STDC_LIB_EXT1__
__STDC_WANT_LIB_EXT1__
1
<stdio.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
int main()
{
char op;
scanf_s("%c", &op, sizeof(op));
if ( op == 'a' )
printf("the character is a");
else
printf("not a");
return 0;
}