0
我一直在編寫一個程序,它接受輸入並檢查數字是偶數還是奇數,並在輸入是字符而不是數字時輸出錯誤消息我的初始代碼爲:在while循環中用scanf()檢查輸入類型
int main()
{
int x;
int check = scanf("%d", &x);
printf("input: ");
while(check != 1){ //means that the input is inappropriate
printf("Error!: unexpected input\n");
printf("input: ");
check = scanf("%d", &x);
}
if(x%2 == 0){
printf("It's even\n");
}else{
printf("It's odd\n");
}
return 0;
}
當我運行一個無限循環的印刷
「錯誤!:意外輸入\ n」個 但是當我把下面的語句在while循環中它工作正常的說法是:scanf("%s",&x);
有人可以解釋這個行爲?