Confused about getchar() function

后端 未结 6 1686
有刺的猬
有刺的猬 2021-02-07 08:25

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 En

相关标签:
6条回答
  • 2021-02-07 08:33

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

    0 讨论(0)
  • 2021-02-07 08:34

    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

    0 讨论(0)
  • 2021-02-07 08:41

    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.

    0 讨论(0)
  • 2021-02-07 08:45

    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)

    0 讨论(0)
  • 2021-02-07 08:47

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

    0 讨论(0)
  • 2021-02-07 08:50

    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.

    0 讨论(0)
提交回复
热议问题