getch() , how to use it in Unix

Hi guys and gals,

i have this code:

#include //Library of time and date functions
#include // added for unix acces to getch()

void displayRandomStars(int n);
char* getpass();

int main ( )
char pass[25]={''};
srand(time(NULL));// seed random generator
printf("Enter password :");
printf("password is %s
", pass);
return 0;


char* getpass()

static char buffer[25]={''}; //password are usually not as big as buffer.
char ch;
int counter = 0;
ch = getch();

displayRandomStars((rand() % 5)+1); //between 1-5 stars
buffer[counter] = ch;
ch = getch();
while (ch != '
return buffer;

void displayRandomStars(int n)

int i = 0;
for (; i < n; i++)


works in windows but in unix i get this:
/tmp/ccyeHYWU.o: In function `myGetpass':
/tmp/ccyeHYWU.o(.text+0x11): undefined reference to `stdscr'
/tmp/ccyeHYWU.o(.text+0x17): undefined reference to `wgetch'
/tmp/ccyeHYWU.o(.text+0x76): undefined reference to `stdscr'
/tmp/ccyeHYWU.o(.text+0x7c): undefined reference to `wgetch'

I'm not even using these functions, anyone know what's going on,
get back to me when you get a chance heavenly ones lol.


  • you're getting the errors because you didn't link to the curses library when you compiled your program. Try:

    gcc program.c -o program -lcurses

    I'm not sure that your program will work because you have to initalise ncurses etc...

    Try this:

    #include // This include stdio.h

    int main()
    initscr(); // Initalise the ncurses library
    cbreak(); // option to disable buffering etc.
    noecho(); // disable the echoing of keystrokes
    printw("Enter Password: "); // print some text to the screen
    refresh(); // update the screen, so the text is displayed
    char pass[25]; // a place to store the text
    getstr(pass); // get text until newline or carriage return
    endwin(); // close ncurses library

    printf("You entered: %s
    ", pass); // print what the user typed to stdout

    return 0;

    compile with:

    gcc prog2.c -o prog2 -lncurses

    for more a tutorial on using the ncurses library see

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion