关于windows:C输入 – getchar()

C input - getchar()

这是一个基本问题……但不得不问。对于这样的程序,如果用例是123^Z,那么程序不会终止,即使我在末尾放了一个eof(ctrl+z)。为什么会这样?只有当我在CR后加上一个EOF,它才起作用。任何安wer都将受到赞赏。谢谢。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include < stdio.h>

void main()
{
    int i, nc;

    nc = 0;
    i = getchar();
    while (i != EOF) {
        nc = nc + 1;
        i = getchar();
    }
    printf("Number of characters in file = %d
"
, nc);
}


(P)在Windows,The CTRL-Z shortcut will only take effect if it's pressed at the start of a line.Otherwise,the os ignores it.你必须出版"Enter"或"Return"才能插入一个新的特征第一。(p)(P)In UNIX,the CTRL-D shortcut will flush EDOCX1 penographic 0 namal/immediately(as mentioned in the below comments),but will not cause EDOCX1 penographic 1 to return EDOCX1 penographic 2 original.Unless you are on a new,blank line;same as in windows.(p)(P)From the comments(below):(p)布尔奇1(P)1 This addresses a good point-no file actually contains EDOCX1 original 2-and pressing ctrl-d won't"insert"(as I had previously said)any thing into the EDOCX1 original 0 stream.这只是一个字母EDOCX1 2 is a standard macro representing a notification that the end of the file was reached by a standard function.(p)(P)Thanks [email protected] the explanation about EDOCX1 penal 2.(p)