对 getchar() 函数感到困惑

发布于 2024-08-24 05:29:11 字数 365 浏览 9 评论 0原文

我对以下代码中 getchar() 的作用感到困惑。我的意思是我知道它可以帮助我查看输出窗口,该窗口只有在我按 Enter 键时才会关闭。

所以 getchar() 基本上是在等待我按 Enter 键,然后读取单个字符。

该函数正在读取的单个字符是什么?我没有按键盘上的任何键来读取它。

现在,当它没有读取任何内容时,为什么它不会给出错误消息“嘿,你没有输入任何内容供我读取”?

#include <stdio.h>

int main()
{
    printf( "blah \n" );
    getchar();
    return 0;
}

I am confused about getchar()'s role in the following code. I mean I know it's helping me see the output window which will only be closed when I press the Enter key.

So getchar() is basically waiting for me to press enter and then reads a single character.

What is that single character this function is reading? I did not press any key from the keyboard for it to read.

Now when it's not reading anything, why does it not give an error saying "hey, you didn't enter anything for me to read"?

#include <stdio.h>

int main()
{
    printf( "blah \n" );
    getchar();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我的黑色迷你裙 2024-08-31 05:29:11

这是因为 getchar() 是一个阻塞函数。

您应该阅读阻塞函数,基本上使进程等待发生某些事情。

这种等待行为的实现取决于函数,但通常它是一个等待某些事件发生的循环。

对于 getchar() 函数的情况,这可能是作为一个循环来实现的,该循环不断读取文件(在本例中为 stdin)并检查文件是否被修改。如果文件被修改,循环将执行其他操作。

That's because getchar() is a blocking function.

You should read about blocking functions, which basically make the process wait for something to happen.

The implementation of this waiting behavior depends on the function, but usually it's a loop that waits for some event to happen.

For the case of the getchar() function, this probably is implemented as a loop that constantly reads a file (stdin for this case) and checks weather the file is modified. If the file is modified, the loop behaves by doing something else.

羁客 2024-08-31 05:29:11

getchar() 函数将简单地等待,直到接收到一个字符,从而使程序保持运行状态。

当您按下回车键时,会发送一个字符;在Windows操作系统下,它将发送一个回车符(CR)和一个换行符(LF)。

请参阅这篇 CodingHorror 帖子,了解详细的解释。

(...CR+LF 部分的解释,而不是 getchar() 阻塞部分)

The getchar() function will simply wait until it receives a character, holding the program up until it does.

A character is sent when you hit the enter key; under a Windows OS, it will send a carriage return (CR) and a line-feed (LF).

See this CodingHorror post for a nicely put explanation.

(...the explanation of the CR+LF part, not the getchar() blocking part)

怪我入戏太深 2024-08-31 05:29:11

试试这个:

#include <stdio.h>

int main(int argc, char *argv[])
{
    char ch;

    printf("I'm now going to block until you press something and then return... ");

    ch = getchar();

    if (ch >= 0)
       printf("\nYou pressed %c\n", ch);
    else
       printf("\nAliens have taken over standard input! Run!\n");

    return 0;
}

getchar() 将使您的程序进入睡眠状态,直到收到键盘(或附加到标准输入的任何内容)中断。这意味着它是阻塞的,在 getchar() 返回之前不会执行任何其他代码。

查看函数的返回值对于理解它非常非常有帮助。

任何函数都可能阻塞,除非它提供某种机制来防止阻塞。例如,open() 允许使用 O_NONBLOCK 标志,这有助于打开调制解调器等响应缓慢的设备。简而言之,如果它从终端获取输入或必须等待从内核或某些设备获取答案,那么它很有可能会阻塞。

Try this:

#include <stdio.h>

int main(int argc, char *argv[])
{
    char ch;

    printf("I'm now going to block until you press something and then return... ");

    ch = getchar();

    if (ch >= 0)
       printf("\nYou pressed %c\n", ch);
    else
       printf("\nAliens have taken over standard input! Run!\n");

    return 0;
}

getchar() will cause your program to go to sleep until a keyboard (or whatever is attached to stdin) interrupt is received. This means it's blocking, no additional code will execute until getchar() returns.

It's very, very helpful to look at the return value of a function in order to understand it.

Any function may block, unless it provides some mechanism to prevent blocking. For instance, open() allows a O_NONBLOCK flag which is helpful for opening slow to respond devices like modems. In short, if it gets input from a terminal or has to wait to get an answer from the kernel or some device, there's a very good chance it might block.

扶醉桌前 2024-08-31 05:29:11

getchar() 会阻止程序执行,直到按下某个键为止。因此,如果没有按下任何键,则不会出现错误, getchar() 将等待它发生:)

getchar() blocks your program's execution until a key is pressed. So, there's no error if no key is pressed, getchar() will wait for it to happen :)

如何视而不见 2024-08-31 05:29:11

您可以在此处了解有关 getchar 行为方式的更多信息:
http://www.cppreference.com/wiki/c/io/getchar
...这应该回答你的问题:)

You can learn more about how getchar behaves here:
http://www.cppreference.com/wiki/c/io/getchar
...this should answer your question:)

诠释孤独 2024-08-31 05:29:11

我认为让您感到困惑的是在程序继续之前需要输入键。默认情况下,终端将缓冲所有信息,直到按下 Enter 键,然后再将其发送到 C 程序。

请参阅此处对 Enter 问题的讨论

I think what confuses you is that the Enter key is needed befor the program continues. By default, the terminal will buffer all information until Enter is pressed, before even sending it to the C program.

see discussion of Enter problem here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文