Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is getch not portable?

Tags:

c

What makes getch inherently unportable to be included as a standard C function?

It is so intuitive and elegant for console interfaces. Without it, asking for a single character is always misleading because users are allowed to enter more than one key.

Worse, you are often required to make sure to clear standard input after reading console input, which isn't even provided as a standard feature! I have to use my own!

A simple program which could be:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> 
#include <conio.h>

int main(int argc, char **argv)
{   
    while (1) {
        char yes_or_no;        

        printf("is this statement correct? 1 + 1 = 2(Y/N) $ ");
        yes_or_no = tolower(getch());

        switch (yes_or_no) {
            case 'y':
                puts("Right!");     
                goto done;

            case 'n':
                puts("\nhint> Please just say yes for the sake of this demo...");
                break;

            case 'q':
                puts("\nExitting.");
                goto done;

            case EOF:
                puts("\nEOF.");
                goto done;

            default:
                printf("\nunknown response '%c'.\n", yes_or_no);
                break;
        }
    }


done:
    return 0;
}

becomes:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> 

static inline void flush_stdin()
{
    char ch;

    do {
        ch = getchar();
    } while ((ch != '\n') && (ch != EOF));
}

int main(int argc, char **argv)
{   
    while (1) {
        char yes_or_no;        

        printf("is this statement correct? 1 + 1 = 2(Y/N) $ ");
        yes_or_no = tolower(getchar());


        switch (yes_or_no) {
            case 'y':
                puts("Right!");     
                goto done;

            case 'n':
                puts("hint> Please just say yes for the sake of this demo...");
                break;

            case EOF:
                puts("EOF.");
                goto done;

            default:
                printf("unknown response '%c'.\n", yes_or_no);
                break;
        }
        flush_stdin(); /* remove this to see the difference */        
    }


done:
    return 0;
}

Every time I want to make a simple portable console program, I feel forced into making a bunch of functions like that, and still end up not having all the things I want, like getch.

Sure, you could use curses, but curses takes over your whole console, and makes your program behave differently than what a regular user expects(for the program to just scroll, and still show the command you ran the program with in the console buffer).

I know "why" is a bad question(what should always be prefered), but it must be asked. Is there anything inherently unportable in getch that a desktop system can't support? If there isn't, I can just write my own and port it to all the platforms I want to support and I'm good.

If there is something getch does that can't be supported by a desktop system, what is it? So I have better reason for understanding why conio is avoided than "just use ncurses/conio is not standard".

like image 615
Dmitry Avatar asked Apr 12 '16 19:04

Dmitry


People also ask

What happen when getch() is not used in programming?

Important Points regarding getch() method: It does not use any buffer to store the input character. The entered character is immediately returned without waiting for the enter key. The entered character does not show up on the console.

Is conio deprecated?

<conio. h> itself belongs to C, and the functions in it are mostly deprecated by newer compilers. Some compilers can still getch(), but not clrscr(). Just try not to touch anything included in the header.

What happen when getch() is not used in programming a output will be 0 b no output will be there C output screen will not be stationary d none?

If getch() function is not used and any scanf function is used in the program. Then after getting input from the input device, the screen blinks in a second and goes off. So, we can't see the output of the program and we should a key to see the output.

Is Getch obsolete?

The Microsoft-specific function name getch is a deprecated alias for the _getch function. By default, it generates Compiler warning (level 3) C4996. The name is deprecated because it doesn't follow the Standard C rules for implementation-specific names. However, the function is still supported.


1 Answers

The C standard has no notion of terminals or Windows. Anything pertaining to these things is implementation specific and cannot be part of the C programming language.

However, there are industry standards for terminal programming. One is part of POSIX (IEEE 1003.1) as the termios terminal driver interface, one is X/Open curses which specifies a library of C functions to manipulate the terminal on a higher level and the third is ISO 6429, the set of ANSI terminal escape sequences.

Incidentally, X/Open curses provides a getch() function. Microsoft Windows also supports these standards but not in a useful way.

The way you would use getch() on Windows (i.e. via conio.h) is not portable though as conio.h is a DOS specific header that cannot easily be implemented on other platforms due to the different model of how the console works in DOS vs. terminals on other platforms.

like image 177
fuz Avatar answered Sep 21 '22 22:09

fuz