0

How can I replace conio.h? Unlike other similar functions, it does not require pressing Enter. Are there any analogues from the standard library?

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

int main(void)
{
    char c;
    unsigned i = 0;

    while (1) {
        c = getch();
        printf("%d - %c", i, c);
        ++i;
    }

    return 0;
}
12iq
  • 83
  • 5
  • Look at ncurses library and ncurses.h – ulix Jul 30 '22 at 18:19
  • 2
    *"Are there any analogues from the standard library?"* - No. It's the main reason libs like conio have lived to such an age. Go with ncurses if you (a) **really** need this functionality, and (b) don't care about the probability of losing IO redirection to your program, and (c) want some form of reasonable portability. There are platform-dependent options, of course (ex: Windows Console API), if that's your cup of tea. – WhozCraig Jul 30 '22 at 18:37

1 Answers1

0

it does not require pressing Enter

NCurses doesn't require pressing Enter too. This is your code based on NCurses which allows that:

#include <ncurses.h>

int main(void) {  
    char c;
    unsigned i = 0;

    initscr();
    noecho();

    while (1) {
        c = getch();
        printw("%d - %c", i, c);
        ++i;
    }

    endwin();
    return 0;
}

To compile don't forget to include "-lncurses", eg:

gcc main.c -lncurses -o main.exe
igroglaz
  • 85
  • 8