tags:

views:

388

answers:

5

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;
}
+8  A: 

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.

Luca Matteis
I think I'll read up on them as well. Is that (based on the user's description) more like an event listener or an interrupt (or neither and I'm the one with the dumb questions)?
Anthony
@Anthony: The internal implementation differs: When you directly access HW (as in reading from a serial port), it will usually be interrupt-driven. If you are reading from a console window in a GUI environment, getchar will probably receive some kind of event from the GUI framework.
sleske
+2  A: 

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

Roman D
+4  A: 

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)

detly
+1  A: 

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

Gabriel Ščerbák
+3  A: 

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.

Tim Post