1

当我输入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");
4

3 回答 3

2

尝试scanf()代替scanf_s().

于 2015-10-03T15:05:32.783 回答
2

说明符%c(另外两个这样的例外%s%[)需要第三个参数大小-

scanf_s("%c", &op, 1);   // 1 to read single character
于 2015-10-03T15:05:51.657 回答
0

第三个参数应该是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;
}
于 2015-10-03T15:27:46.940 回答