Unbuffered input from keyboard

When I use getchar() in my C programs, it does not get the characters as I type in.  It waits for a carriage return.  The function call setbuf(stdin, NULL) did not help.  What do I have to do so that the application program does not have to wait until the user types a carriage return ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try using getch() from curses.h
I don't have unix machine to test this. But I am sure that will work.
NathAuthor Commented:
This did not work.  It gives the following error messages:
undefined reference to stdscr
undefined reference to wgetch
did you link with -lcurses
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Below is a C++ program that demonstrates how to read single keys.

#include <stdio.h>
#include <sys/termio.h>   // for HP_UX
#include <unistd.h>
#include <sys/ioctl.h>

struct termio zap, original;

// Read a character (or none) from a file descriptor.
// min:     minumum number of characters that "read" is waiting // for
// timeout: maximum waiting time (in units of 0.1 second)
// Returns:
// -2 on error
// -1 if no key was pressed
// else keycode

int readOneCharacter (int fd, int min, int timeout) {

    unsigned char c;
    // Must be unsigned because we want to return character     // values in the
    // range 0..255.
    int count;
    int rc;
    // Modify termio
    zap = original;
    zap.c_cc [VMIN] = min;
    zap.c_cc [VTIME] = timeout;

    // ignore Ctrl-C (interrupt), Ctrl-Z (suspend) and Ctrl-\ (quit)
    zap.c_lflag = 0;

    // recognize Ctrl-C (interrupt), Ctrl-Z (suspend) and Ctrl-\         // (quit)
    // zap.c_lflag = ISIG;  

    // disable flow control (Ctrl-S, Ctrl-Q), do not convert CR to LF             // or vice versa.
    zap.c_iflag = 0;
    // Set new termio
    rc = ioctl (fd, TCSETA, &zap);
    if (rc == -1) {
        perror ("ioctl");
        return -2;

    // Read one char
    count = read (fd, &c, 1);
    if (count == -1) {
        // An error has occured
        perror ("read");
        return -2;
    } else if (count == 1) {
        // A key has been pressed. Return the key.
        return int (c);
    } else {
        // No key pressed.
        return -1;

// Wait for a key to be pressed. If the timeout expires and no // key was pressed, -1 is returned.
int kbGetKeyTimeout (int timeout) {
    return readOneCharacter (STDIN_FILENO, 0, timeout);

// Wait for a key to be pressed
int kbGetKey () {
    return readOneCharacter (STDIN_FILENO, 1, 0);

// If a key was pressed, then return it, otherwise return -1.
// Don't wait.
int kbGetKeyPressed () {
    return readOneCharacter (STDIN_FILENO, 0, 0);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NathAuthor Commented:
This program works.  But, it does not reset the original termio and, therefore, stays in the new mode after the function is called.  How do we reset the original termio ?
Oops, I forgot to paste the initialization stuff. Sorry! Here it is:

int main () {

    // Sanity check
    if  (!isatty (STDIN_FILENO)) {
        fprintf (stderr, "stdin: not a tty.\n");
        return 1;

    // Read termio
    ioctl (STDIN_FILENO, TCGETA, &original);

    // Now read the keys....
    // ....
    ... kbGetKey()  ....
    // ....

    // Restore original termio
    ioctl (STDOUT_FILENO, TCSETA, &original);

    return 0;

Both steps (read original termio, restore original termio) can also be performed at the beginning and the end of function "readOneCharacter", respectively. But if you plan to read more than a few keys, it is advisable to leave it in the main routine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.