在以下代码中,为什么ch2
和ch3
的值等于-1
?
char ch1;
char ch2;
char ch3;
printf("put Type: ");
ch1 = getchar();
_flushall();
printf("put Type: ");
ch2 = getchar();
_flushall();
printf("put Type: ");
ch3 = getchar();
printf("\n");
printf("the ascii value for this three types is: %d, %d, %d", ch1, ch2, ch3);
答案 0 :(得分:3)
如果getchar
遇到错误,则会返回EOF
(这是一个扩展为int
且具有负值的宏,通常为“-1”)。这就解释了为什么您会看到-1
。
至于为什么 getchar
失败,这是一个单独的问题。通常,这意味着它已到达输入流的末尾。我认为这与你用_flushall
冲洗它的事实有关吗?