0

I had installed ncurses library in Linux mint and still I can't use getch function in . I am using Linux mint 18.2.

Here is my program:

#include <stdio.h>
#include <curses.h>

int main() {
    char k;
    printf("how are you");

    k = getch();
    printf("%c",k);
}

and here is the output:

 ram@ram$ gcc-7 test.c -lcurses
 ram@ram$ ./a.out 
 how are you�ram@ram$

It does't wait for me to press any key and terminate to quickly. I don't want to install conio.h for Linux. How can I use getch and getche function in Linux? Please don't tell me to make my own function. I am still a noob. Or there must be alternatives.

  • 1
    `getch()` has only defined behavior after you initialized curses. Google for at least `initscr()` and `endwin()`. –  Jul 31 '17 at 10:51
  • 1
    Btw, you can't mix `stdio` functions with `curses` functions. –  Jul 31 '17 at 10:52
  • More importantly: Why? what do you want to achieve? From the current problem statement, it does not look like you need ncurses for anything. So? – Sourav Ghosh Jul 31 '17 at 10:56
  • 3
    Possible duplicate of [Why getch() returns before press any key?](https://stackoverflow.com/questions/7410447/why-getch-returns-before-press-any-key) – Gaurav Pathak Jul 31 '17 at 10:56
  • maybe you want to use `getchar()` instead? – Chris Turner Jul 31 '17 at 11:05
  • @ChrisTurner No –  Jul 31 '17 at 11:06
  • @voldimot Please use **bold** only for things that are indeed especially important to your question/answer. And you should probably have a look at the proposed duplicate, it's a very good match of your problem. –  Jul 31 '17 at 11:24
  • @FelixPalmen Is there any alternative function? –  Jul 31 '17 at 11:40
  • @voldimot what do you consider "alternative"? There's the C standard function `getchar()` coming somewhat close. –  Jul 31 '17 at 11:41
  • @FelixPalmen In` getchar` user have to press enter.but I don't want to user to have to press enter. –  Jul 31 '17 at 11:45
  • @FelixPalmen I think I should build a function. –  Jul 31 '17 at 11:45
  • @voldimot on Linux, you could probably just use `char k; read(0, &k, 1);` (with `unistd.h` included), but I don't think this is very portable. If you want some terminal control and a guarantee that no buffer gets into your way, using `curses` is probably the best bet, see my answer and the linked duplicate. –  Jul 31 '17 at 12:11
  • @FelixPalmen Nope. If the terminal is in buffered mode, `read` will not return until ctrl-D or the return key is pressed. – JeremyP Jul 31 '17 at 12:37
  • @JeremyP and that's outside of the control of standard C as well, yes ... I probably shouldn't even have mentioned `read()`. [Here's a better reference](https://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar) –  Jul 31 '17 at 12:40

1 Answers1

3

Here's a "corrected" version, explaining what's wrong in the comments:

#include <curses.h>
#include <stdio.h>

int main(void)
{
    // use the correct type, see https://linux.die.net/man/3/getch
    int k;

    // init curses:
    initscr();

    // in curses, you have to use curses functions for all terminal I/O
    addstr("How are you?");

    k = getch();

    // end curses:
    endwin();

    printf("You entered %c\n", k);

    return 0;
}

This still isn't good code, you should at least check whether you got a valid character from getch().

It's also important to note that getch() isn't a "function of C". It's part of curses, a well-known platform-independent API for console/terminal control, with implementations e.g. for *nix systems (ncurses) and Windows (pdcurses). It's not part of the language C.