【发布时间】:2020-07-02 04:41:32
【问题描述】:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
当我运行这个程序并为 ex 'helloworld' 编写一个字符串并按 Ctrl + D 时。它会再次打印我的字符串 - 'helloworldhelloworld'。
然后我再次打印 Ctrl + D 并显示:
helloworldhelloworld^D
Count: 10
为什么 Ctrl + D 不能立即打印计数? 在 Mac OS 上使用 Visual Studio Code。
编辑:我发布了错误的代码。真的很抱歉。
#include <stdio.h>
main()
{
int c;
int counter_a = 0;
c = getchar();
while(c != EOF)
{
putchar(c);
c = getchar();
counter_a = counter_a + 1;
}
printf("\nCount: \t%d\n", counter_a);
}
这是我正在运行的代码。所以我的问题解决了。
【问题讨论】:
-
这个程序不应该打印任何字符串。删除可执行文件,然后重新编译并确保您正在运行正确的程序。
-
请务必标记您正在使用的编程语言。像我这样的人会忽略不相关的标签,因此我们可以帮助人们处理我们知道的事情。 ;-)
-
它工作正常,Ctrl-d 将停止接收进一步的输入流。请参考 C 上的FAQ。