哪里'的getchar()`存储用户输入? [英] Where does `getchar()` store the user input?

查看:176
本文介绍了哪里'的getchar()`存储用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始阅读 C编程语言(K&安培; R)我有一个关于的getchar()功能毋庸置疑。

I've started reading "The C Programming Language" (K&R) and I have a doubt about the getchar() function.

例如这个code:

#include <stdio.h>

main()
{
  int c;

  c = getchar();
  putchar(c);
  printf("\n");   
}

键入 toomanychars + <大骨节病> CTRL + <大骨节病> D (EOF)打印刚 T 。我认为这意料之中的,因为它推出的第一个字符。

Typing toomanychars + CTRL+D (EOF) prints just t. I think that's expected since it's the first character introduced.

但随后的另一块code的:

But then this other piece of code:

#include <stdio.h>

main()
{
  int c;

  while((c = getchar()) != EOF) 
    putchar(c);
}

键入 toomanychars + <大骨节病> CTRL + <大骨节病> D (EOF)打印 toomanychars

Typing toomanychars + CTRL+D (EOF) prints toomanychars.

我的问题是,为什么出现这种情况,如果我只有一个字符变量?在哪里其余字符存储?

My question is, why does this happens if I only have a single char variable? where are the rest of the characters stored?

编辑:

感谢大家的答案,我开始变得现在...只有一个陷阱:

Thanks to everyone for the answers, I start to get it now... only one catch:

定时的第一程序退出 CTRL + D 而第二打印的整个字符串,然后等待更多的用户输入。为什么它会等待另一个字符串,并且不退出像第一?

The first program exits when given CTRL+D while the second prints the whole string and then waits for more user input. Why does it waits for another string and does not exit like the first?

推荐答案

它就像对待一个文件的输入流。这是因为如果你打开​​了一个文件,其中包含文本toomanychars,并读取或输出它一次一个字符。

It's treating the input stream like a file. It is as if you opened a file containing the text "toomanychars" and read or outputted it one character at a time.

在第一个例子,在没有while循环的,这就像你打开一个文件并读取第一个字符,然后输出它。但是第二个例子将继续读取字符,直到它得到文件结束信号( CTRL + D 你的情况)就像如果它是从磁盘上的文件读取。

In the first example, in the absence of a while loop, it's like you opened a file and read the first character, and then outputted it. However the second example will continue to read characters until it gets an end of file signal (ctrl+D in your case) just like if it were reading from a file on disk.

在回答你的问题的更新,你使用的是什么操作系统?我跑了它在我的Windows XP的笔记本电脑,它工作得很好。如果我打输入,它会打印出什么,我到目前为止,创建一个新行,然后继续。 (在的getchar()函数不返回,直到你preSS进入,这是时,有没有在输入缓冲区,当它被称为)。当我preSS CTRL + Z (EOF在Windows中),程序将终止。请注意,在Windows中,EOF必须在自己的行数在命令提示符下一个EOF。我不知道这种行为是在Linux的模仿,或者您可能正在运行的任何系统。

In reply to your updated question, what operating system are you using? I ran it on my Windows XP laptop and it worked fine. If I hit enter, it would print out what I had so far, make a new line, and then continue. (The getchar() function doesn't return until you press enter, which is when there is nothing in the input buffer when it's called). When I press CTRL+Z (EOF in Windows), the program terminates. Note that in Windows, the EOF must be on a line of its own to count as an EOF in the command prompt. I don't know if this behavior is mimicked in Linux, or whatever system you may be running.

这篇关于哪里'的getchar()`存储用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆