Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么C程序打印0D而不是0?(当EOF作为Ctrl+;D发送时)_C_Macos - Fatal编程技术网

为什么C程序打印0D而不是0?(当EOF作为Ctrl+;D发送时)

为什么C程序打印0D而不是0?(当EOF作为Ctrl+;D发送时),c,macos,C,Macos,OSX 10.6.8,GCC 4.2 86_64 #include <stdio.h> /* count lines in input */ main() { int c, nl; nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl); } 按ctrl+d键发送EOF 0D 应该是0。为什么它会附加附加

OSX 10.6.8,GCC 4.2 86_64

#include <stdio.h>

/* count lines in input */
main()
{
    int c, nl;

    nl = 0;
    while ((c = getchar()) != EOF)
        if (c == '\n')
            ++nl;
    printf("%d\n", nl);
}
按ctrl+d键发送EOF

0D

应该是0。为什么它会附加附加?这是什么意思?

我看过这部电影,它也把我弄糊涂了

终端回显
^D
,然后程序输出
0
,覆盖插入符号

您可以通过将程序中的打印格式更改为
“\n%d\n”
来演示这一点


当被问到“为什么?”时,我去探索。答案在tty设置中。对于我的终端,
stty-a
的输出为:

speed 9600 baud; 65 rows; 120 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
    -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
    -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
    -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
    -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
    eol2 = <undef>; erase = ^?; intr = ^C; kill = ^X; lnext = ^V;
    min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
    stop = ^S; susp = ^Z; time = 0; werase = ^W;
您看不到它,但是对于每个
cat
命令,我都在
asd
字符行的末尾键入了一个Control-D,在点击return之后键入了第二个。提示删除了第二个示例中的第二个回声
^D

因此,如果您不喜欢回显控件字符,请关闭回显:

stty -echoctl
炮弹也会挡住去路;我尝试了Control-R,我的shell(
bash
)决定进入

(reverse-i-search)`': aasadasdadadasdadadadadadsad

我输入了“asd”字符的非原始序列,然后输入了Control-R,这就是我在shell中结束的地方。我打断了他的话;我不确定反向搜索是什么,但我怀疑它是Emacs-ish;这不是我所期望的。

在Mac OSX上运行Xcode得到了以下输出:

第一行
第二行
现在我要在返回后按控制按钮D。

3

如果按ctrl+D,则听起来D来自终端。。。e、 g.它与printf(或代码?)无关。如果你试着说,
more
less
,会发生什么?如果你将输入输入到程序中呢?是每次你用CTR D发送EOF时都会发生这种情况吗?是发送到任何程序,还是仅仅发送到这个测试?我想除了
0
之外的
D
就是来自上面的组合键machine@pst不,D不是从那个里来的-ctrl+D也是发送EOFSee的快捷方式:。哇,真是太棒了!但为什么只有在这种特殊情况下才会发生呢?还是一直都是这样?为什么^D会被回响?非常感谢你做了所有这些研究!看到实现非常有趣。Xcode不是bash,所以它可能不会像Jonathan解释的那样输出^D。这可能是您的tty设置:查看
[-]echoctl的
stty-a
输出;如果您有
-echoctl
,则不会看到
^D
;如果您有
echoctl
,您将。对我来说,这是
stty-a
输出的第2行标记为“lflags:”的行的末尾。
stty -echoctl
(reverse-i-search)`': aasadasdadadasdadadadadadsad