Confused about getchar() function

蓝咒 提交于 2019-12-04 10:29:21

问题


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;
}

回答1:


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)




回答2:


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.




回答3:


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.




回答4:


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 :)




回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/2408140/confused-about-getchar-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!