Is there any standard function in gcc that is equivalent to getch() so that I can read the input without hitting enter key?

I assume you're using a Linux system and not MinGW because MinGW's gcc supports getch(). You can reproduce the effect of getch() on Linux like this.

Comments
On Linux, using gcc 4.4.5; getch() works for me!

Since getch() is not and never has been part of the language definition, you should consider not using it at all.

The link claims the conio.h is deprecated. It is not deprecated. It has never been defined by C nor C++. It's simply an add-on header extremely few compilers defined.

Since getch() is not and never has been part of the language definition, you should consider not using it at all.

The language definition doesn't have an equivalent, so unless you're sure the behavior isn't needed, this is a bad argument for such advice. Depending on how getch() is used, it may or may not be superfluous though. If it's superfluous then there are grounds for removing the feature entirely in favor of portability. But that's not really your call, is it? ;)

Edited 4 Years Ago by deceptikon: n/a

The language definition doesn't have an equivalent, so unless you're sure the behavior isn't needed, this is a bad argument for such advice. Depending on how getch() is used, it may or may not be superfluous though. If it's superfluous then there are grounds for removing the feature entirely in favor of portability. But that's not really your call, is it? ;)

Hence my phrasing: "you should consider not using it at all" :icon_wink:

I assume you're using a Linux system and not MinGW because MinGW's gcc supports getch(). You can reproduce the effect of getch() on Linux like this.

This is great! I thought I could only do it using the ncurses library. Thank you, deceptikon.

Built using:

andy@debian:/usr/include$ gcc --version
gcc (Debian 4.4.5-8) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

just save this as conio.h to use getch() and getche() functions :

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

/* reads from keypress, echoes */
int getche(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

Edited 4 Years Ago by deceptikon: Fixed code tags

This article has been dead for over six months. Start a new discussion instead.