简体   繁体   English

C编程 - 循环

[英]C programming — loop

I am following the exercises in the C language book. 我正在关注C语言书中的练习。 I am in the first chapter where he introduces loops. 我在第一章介绍了循环。 In this code: 在这段代码中:

#include <stdio.h>

/* copy input to output; 1st version */

int main() {
  int c, n1;

  n1 = 0;
  while ((c = getchar()) != EOF) {
    if (c == '\n') {
      ++n1;
    }
    printf("%d\n", n1);
  }
}

In here I am counting the number of lines. 在这里,我计算行数。 When I just hit enter without entering anything else I get the right number of lines but when I enter a character then hit enter key the loop runs twice without asking for an input the second time. 当我在没有输入任何其他内容的情况下点击输入时,我得到正确的行数,但是当我输入一个字符然后按回车键时,循环运行两次而不需要第二次输入。 I get two outputs. 我得到两个输出。 this how the output looks like: 这个输出如何:

   // I only hit enter
1
   // I only hit enter
2
   // I only hit enter
3
g // I put char 'g' then hit enter
3
4

3 and 4 print at the same time. 3和4同时打印。 why is 4 printing after the loop has been iterated already? 为什么循环迭代后已经进行了4次打印? I thought the loop would restart and ask me for input before printing 4. 我认为循环会重新启动并在打印之前询问我输入4。

The getchar function reads one character at a time. getchar函数一次读取一个字符。 The number of lines will be printed for every character in the input read by getchar , whether that character is newline or not, but the counter will only be incremented when there is a newline character in the input. 对于getchar读取的输入中的每个字符,将打印行数,无论该字符是否为换行符,但只有在输入中有换行符时才会递增计数器。

When you enter g then the actual input that goes to the standard input is g\\n , and getchar will read this input in two iterations and that's the reason it is printing number of lines twice. 当你输入g ,转到标准输入的实际输入是g\\n ,而getchar将在两次迭代中读取这个输入,这就是它打印两次行数的原因。

If you put the print statement inside the if block then it will print only for newline characters. 如果将print语句放在if块中,那么它将仅为换行符打印。 If you put the print statement outside the loop, then it will only print the count of the number of lines at the end of the input. 如果将print语句放在循环之外,那么它只会打印输入结尾处的行数。

To be clear this is the terminal that you are dealing with. 要明确这是你正在处理的终端。

By default, the terminal will not get input from the user \\n is entered. 默认情况下,终端不会从用户输入\\n输入。 Then the whole line is placed in the stdin . 然后整行被放置在stdin

Now as I said earlier here the program is not affected by the buffering of stdin . 现在正如我之前所说,程序不受stdin缓冲的影响。 And then the characters will be taken as input and it is processed as you expect it to be. 然后将字符作为输入,并按照您的预期进行处理。 The only hitch was the terminals buffering - line buffering. 唯一的障碍是终端缓冲线缓冲。

And here from standard you will see how getchar behaves:- 从标准中你可以看到getchar行为: -

The getchar function returns the next character from the input stream pointed to by stdin . getchar函数返回stdin指向的输入流中的下一个字符。 If the stream is at end-of-file, the end-of-file indicator for the stream is set and getchar returns EOF . 如果流位于文件结尾,则设置流的文件结束指示符,并且getchar返回EOF If a read error occurs, the error indicator for the stream is set and getchar returns EOF. 如果发生读取错误,则设置流的错误指示符并且getchar返回EOF。

Now what are those characters - those charaacters include \\n - the \\n is what you put in the terminal and then to stdin via pressing the ENTER . 现在这些角色是什么 - 那些角色包括\\n - \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n你放入终端然后通过按ENTER键转到stdin Here earlier you were entering the characters earlier which were \\n . 在前面你输入的字符是\\n This time you entered two characters. 这次你输入了两个字符。 That's why the behavior you saw. 这就是你看到的行为的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM