Home » C programs » C misc. programs

C Example to use 'kbhit' function

In this example, we are going to learn about 'kbhit()' function - which is a predefined function of 'conio.h' header file with an example.
Submitted by Shamikh Faraz, on February 25, 2018

kbhit() is a function used to check whether a key is pressed or not. To use 'kbhit' function you must include <conio.h> header file. If a key is pressed then it returns a non zero value, otherwise returns zero.



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

int main()
{
	do
	{
		printf("Press any key to stop loop.\n");
	} while (!kbhit());

	return 0;
}

Output

Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.
Press any key to stop loop.

Till N time, until you don't press any key.

Until you don’t press any key, the condition in do while loop is true and continuously prints "Press any key to stop loop." When any key is pressed, the condition in do while loop goes false and kbhit() will return a non-zero value. (!(any_non_zero) = 0), so do while loop stops.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.