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

Recommended Answers

All 6 Replies

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.

commented: On Linux, using gcc 4.4.5; getch() works for me! +0

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? ;)

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.

Member Avatar for jaybhanderi

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.