c - 使用“getchar()”和“EOF”期间的无意义输出流
问题描述
我一直在尝试getchar()
,putchar()
并且一直在尝试使用EOF
. 下面是我一直在试验的代码片段。
#include <stdio.h>
int main(void)
{
int c;
c = getchar();
while(c != EOF)
{
putchar(c);
printf("\n");
printf("%d\n", EOF);
c = getchar();
}
return 0;
}
输入: -
一个
预期输出:-
一个
//Due to putchar()
-1
//Value of EOF
//Now the cursor should come in next line and wait for next character.
实时输出:-
一个
-1
-1
//Cursor waiting for next character.
我无法理解输出显示-1
两次的原因。
解决方案
您的代码注释说
//Now the cursor should come in next line and wait for next character.
但是第二个循环不会等待。它读取已经输入的换行符,这由输出中的额外空白行显示。
推荐阅读
- c++ - 当 std::string/char const* 已经在 utf-8 中时,从 std::string/char const* 创建 std::u8string
- python - 如何在 Django Rest Framework 中为外键显示字段值而不是 id,以确保所有字段都像以前一样在 DRF HTML 表单上显示
- mongodb - 电子邮件地址作为 _id 而不是 OID?
- ssl - 使用 ActiveMQ Artemis + TLS 进行 JMeter 测试
- php - 根据键合并数组数据
- python - For 和 while 函数,具有多个终止条件的重复结构
- database - FMX LiveBindings CustomFormat IfThen 具有三个或更多结果
- java - 如何在 Spring 或 EE 上重写 Guice Interface DI Injector.getInjector().getInstance(Interface.class)
- php - 抽象静态方法中的实例子类
- python - 尝试在 for 循环中使用列表索引