Question

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;
}
Was it helpful?

Solution

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)

OTHER TIPS

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.

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.

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top