问题描述
我有一个非常简单的程序,只是打印新行作为一个整数的数量,我每天号码后得到一个D。
样品输入:
D [进入]
E [进入]
F [进入]
按Ctrl-D [进入]
示例输出:
3D
我在做什么错了?
这是逐字从C程序设计语言第2版,页。 19:
的#include<&stdio.h中GT;主要()
{
INT C,NL;
NL = 0; 而((C =的getchar())!= EOF)
如果(C =='\\ n')
++ NL;
的printf(%d个\\ N,NL);
}
我觉得对D ^ D作为其标准的回波逻辑,通过之前对应的字符(或者更确切地说,这里缺乏到控制台输出来自Ctrl键D.焦炭,即EOF状态)来的getchar(),但是,这是理所当然,不发送CR / LF。 C程序然后将其3,等瞧...
通过退出和问题应该走开前打字超过9 CR试用程序,即不显示。
I have a very simple program that just prints the number of newlines as an integer and I get a "D" after every number.
Sample input:
d [enter]
e [enter]
f [enter]
Ctrl-D [enter]
Sample output:
3D
What am I doing wrong?
This is verbatim from The C Programming Language 2nd edition, pg. 19:
#include <stdio.h>
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
I think the D comes from the Ctrl D. The console outputs ^D as its standard echo logic, prior to passing the corresponding char (or rather here lack of char, i.e. EOF status) to getchar(), however, and rightfully, not sending a cr/lf. The C program then sends its 3, et voila...
Try the program by typing more than 9 CR before exiting and the problem should "go away", i.e. not show.
这篇关于简单的程序加入&QUOT; D&QUOT;输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!