Is there any C library function to check the keypress from keyboard( I am working on linux machine ).
-
1Duplicate http://stackoverflow.com/a/6731427/2463134 – erbdex Dec 03 '13 at 11:16
-
possible duplicate of [C exit from infinite loop on keypress](http://stackoverflow.com/questions/6731317/c-exit-from-infinite-loop-on-keypress) – erbdex Dec 03 '13 at 11:16
-
What do you need? It's not clear at all – Guido Dec 03 '13 at 11:26
-
You should've clearly mentioned, that you need 'non-blocking input'. If you want to ask a question on stackoverflow, take a look at "How do I ask a good question" http://stackoverflow.com/help/how-to-ask – Constantin Dec 04 '13 at 08:03
-
Take a minute and rewrite this question, as one finds it easily via searchengine. Thanks! – Aiyion.Prime Feb 22 '15 at 08:05
2 Answers
getchar()
from the Header file stdio.h
returns the next character from stdin. That's probably what you're searching for.
The following code will output the first char from the stdin stream:
#include <stdio.h>
int main (int argc, char **argv){
char c = getchar();
printf("Char: %c", c);
return 0;
}
There are also other functions available to do this without blocking i.e. kbhit()
and getch() in conio.h
. But the header file conio.h
is non-standard and probably not available on your platform if you are using linux.
So you have 2 options:
1.) Using the library ncurses you can use i.e. the function timeout() to define an timeout for the getch()
function like this:
initscr();
timeout(1000);
char c = getch();
endwin();
printf("Char: %c\n", c);
2.) Implement kbhit()
by yourself without using ncurses. There is a great expanation here to do so. You would have to measure time by yourself and looping until your timeout is reached. To measure time in C, there are plenty threads here on stackoverflow - you just have to search for it. Then your code would look like this:
while(pastTime() < YOUR_TIMING_CONSTRAINT){
if (kbhit()){
char c = fgetc(stdin);
printf("Char: %c\n", c);
}
}

- 921
- 7
- 14

- 8,721
- 13
- 75
- 126
-
getchar() function will wait for input from keyboard, if we don't press any key it will continue to be in the same state and program execution stops there itself untill we press a key. But i want a function it should wait for some time if we dont press a key it should come out of that state and rest of program should continue its execution. – user3035481 Dec 04 '13 at 03:59
-
1@user3035481 Sorry, but my crystal ball is broken. If you had clear requirements like that in the first place, you should've posted it in your inital post. Because you hadn't, you made people answer your incomplete question and wasted everybodys time here. However, I updated my answer with infos about non-blocking input. – Constantin Dec 04 '13 at 07:58
You can use getchar()
or getc(stdin)
, these are standard functions in C. They echo the character to the terminal that was pressed.
or even getch()
.The advantage is, it does not echo the characters pressed to the terminal. Note getch()
is not a part of standard C. You could write your own function for getch()
or use curses.h

- 9,285
- 27
- 84
- 131
-
getchar() function will wait for input from keyboard, if we don't press any key it will continue to be in the same state and program execution stops there itself untill we press a key. But i want a function it should wait for some time if we dont press a key it should come out of that state and rest of program should continue its execution. – user3035481 Dec 04 '13 at 04:00