为什么getchar()返回-1?

时间:2013-11-04 12:37:08

标签: c

在以下代码中,为什么ch2ch3的值等于-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);

1 个答案:

答案 0 :(得分:3)

如果getchar遇到错误,则会返回EOF(这是一个扩展为int且具有负值的宏,通常为“-1”)。这就解释了为什么您会看到-1

至于为什么 getchar失败,这是一个单独的问题。通常,这意味着它已到达输入流的末尾。我认为这与你用_flushall冲洗它的事实有关吗?