You are here

kbhit in c

kbhit in c: kbhit function is used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file "conio.h". If a key has been pressed then it returns a non zero value otherwise returns zero.

Declaration : int kbhit();

C programming code for kbhit

  1. #include <stdio.h>
  2. #include <conio.h>
  4. main()
  5. {
  6.    while (!kbhit())
  7.       printf("You haven't pressed a key.\n");
  9.    return 0;
  10. }

As long as in the above program user doesn't presses a key kbhit() return zero and (!0) i.e. 1 the condition in while loop is true and "You haven't pressed a key." will be printed again and again. As a key is pressed from the keyboard the condition in while loop become false as now kbhit() will return a non-zero value and ( !(non-zero) = 0), so the control will come out of the while loop.