16

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Serenity
  • 4,968
  • 19
  • 65
  • 104
  • 2
    getchar() will keep waiting indefinitely until a key is pressed. Its your servant. – N 1.1 Mar 09 '10 at 10:11
  • 4
    Welcome to stackoverflow! Newbie questions are totally OK here, as long as they are otherwise on-topic. Yours is actually a fairly good question, as it addresses an important concept. – sleske Mar 09 '10 at 10:23
  • 1
    Please try and keep your title close to the topic of the question, it will help others who are searching for something similar. I've edited your title to just reflect the question. Welcome to Stack Overflow :) – Tim Post Mar 09 '10 at 10:28
  • Thanks for the welcome :) I will keep in mind what you said about title relevance. So glad I found this website today..been searching for one since long where my queries could be answered real quick...all the answers I got have been of great help..thanks to all of you guys :) – Serenity Mar 09 '10 at 10:33

6 Answers6

11

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
  • 29,161
  • 19
  • 114
  • 169
  • 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 Mar 09 '10 at 10:14
  • @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 Mar 09 '10 at 10:24
9

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
  • 29,332
  • 18
  • 93
  • 152
5

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
  • 33,371
  • 15
  • 110
  • 174
2

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 Dmitrienko
  • 3,375
  • 3
  • 37
  • 48
1

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
  • 18,240
  • 8
  • 37
  • 52
0

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

karsten
  • 639
  • 5
  • 13