[英]Why does getchar() return -1?
在下面的代码中,为什么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);
如果getchar
遇到错误,则返回EOF
(这是一个扩展为带有负值(通常为“ -1”)的int
的宏)。 这解释了为什么您看到-1
。
至于为什么 getchar
失败,这是一个单独的问题。 通常,这意味着它已经到达输入流的末尾。 我认为这与您要使用_flushall
冲洗它_flushall
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.