2015-10-03 19 views
1

當我輸入a時,輸出爲not a。條件是真的那麼爲什麼輸出not a?當我使用getchar而不是scanf_s時,它工作正常。有什麼問題?爲什麼scanf_s函數沒有正確輸入?

char op; 
scanf_s("%c", &op); 
if (op == 'a') { 

printf("the character is a"); 

} 
else { 
printf("not a"); 
} 
+0

您正在使用什麼編譯器? –

+0

我不知道。只要運行它的Visual Studio 2013 –

+2

嘗試'scanf_s( 「%C」,&OP,1);' – BLUEPIXY

回答

2

嘗試scanf()而不是scanf_s()

+0

VS將使用'的scanf時發出一個警告(也許錯誤)()'。這很煩人。 –

+0

@Cool蓋伊http://stackoverflow.com/a/23487039/2410359安靜警告 – chux

2

說明符%c(兩個這樣的例外%s%[)需要大小 -

scanf_s("%c", &op, 1); // 1 to read single character 
0

第三參數應該是sizeof類型第三參數。 scanf_s僅保證可供如果__STDC_LIB_EXT1__由實現所定義,並且如果用戶包括<stdio.h>之前定義__STDC_WANT_LIB_EXT1__爲整數常數1

#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; 
} 
相關問題