苹果终端上按Ctrl-D输出d字符 [英] CTRL-D on Mac terminal outputs a D character
问题描述
我运行这个例子(从Kernighan和Ritchie的C语言书第1.5.2节),在Mac OS X计算机终端上:
I am running this example (from Kernighan and Ritchie's C book section 1.5.2) on a mac OS X machine terminal:
#include <stdio.h>
int main()
{
int c, nl;
nl = 0;
while((c = getchar()) != EOF)
if(c == '\n')
++nl;
printf("%d\n", nl);
}
我运行应用程序,并立即 CTRL-D
进入EOF字符。该项目输出 0D
并终止。 0是预期的输出,但哪里额外的'D'从何而来?
I run the app and enter the EOF character CTRL-D
immediately. The program outputs 0D
and terminates. The 0 is the expected output, but where does the extra 'D' come from?
I saw this thread and this faq, but could not find an answer.
推荐答案
我敢打赌,终端呈现出 ^ D
(正常插入符号符号)和离开的光标插入符号,那么你的程序是印刷 0 \\ n
,因为 NL
0覆盖插入符。
I bet the terminal is showing a ^D
(normal caret notation) and leaving the cursor on the caret, then your program is printing 0\n
because nl
is 0 overwriting the caret.
添加睡眠(5)
前的最后的printf
要证实这一点。
Adding a sleep(5)
before the final printf
should confirm this.
这篇关于苹果终端上按Ctrl-D输出d字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!