At whatever point you compose any C program, you should utilize stdio.h and conio.h header documents to your program. Have you considered what these header records are needed in your program?
There is a great deal of data accessible about stdio.h header document and its capacities on the web however developers frequently battle to get data about the conio.h header record and its capacities.
So in this article, you will find all the conio.h library capacities, test source code, and illustration of conio.h header record.
Let’s start with the overview.
What is conio.h in C/C++:
The conio represents Console-Input-Output. The conio.h is a non-standard header document utilized in C and C++ programming. This record contains console input-yield capacities which are for the most part utilized by MS-DOS compilers.
Here we have clarified a portion of the significant and most broadly utilized elements of conio.h header record. Click on each capacity to explore through each capacity.
Note: Use Turbo C compiler to compile and execute conio.h header file functions.
conio.h header file functions:
clrscr():
Utilizing this capacity you can clear the result order window. On the order brief, we normally print code execution status, mistake data, and result results. During the code execution, to clear the current printed data on the result console then you can utilize clrscr()function.
Function Declaration:
void clrscr(void);
Example:
int main()
{
// Message to be cleared from the screen
printf(“Press any key to clear the screen”);
getch();
clrscr();
// Message on the screen after clearing the previous message
printf(“The previous screen is now cleared.\n”);
getch();
return 0;}
getch():
Utilize this capacity to peruse characters from the console. This capacity is additionally used to hold the result screen until the client enters any person. Assuming you don’t utilize this capacity then the result screen closes inside a negligible part of a second.
The getch() is a non-standard capacity gave by conio.h though getchar() is a standard c library work.
Function Declaration:
int getch(void);
Example:
int main()
{
printf(“Press any key to continue…”);
getch();
return 0;}
getche():
This capacity is like getch() work. The main distinction is that this capacity additionally prints the worth entered by the client in the result window
Function Declaration:
char getche();
Example:
int main()
{
printf(“Do you want to go? Y or N”)
getche();
return 0;}
Output:
Do you want to go? Y or NY
putch():
Utilize this capacity to print data in the result window. This capacity prints just each character in turn on yield window.
Function Declaration:
int putch (int c);
Example:
int main()
{
char ch =’v’;
putch(ch);}
Output:
v
cgets():
Utilize this capacity to peruse a series of characters from the control center. This capacity peruses characters until it experiences carriage-return (CR) and linefeed (LF). The cgets() function replaces CR/LF with the null terminator (\0) at the end of the string.
Function Declaration:
char* cgets(char* str);
Example:
int main(void)
{
char buffer[83];
char *p; buffer[0] = 81;
printf(“Enter some characters:”);
p = cgets(buffer);
printf(“\n Entered characters: \”%s\”\n”,p);
return 0; }
cputs():
Utilize this capacity to print a series of characters on the result screen. The carriage-return and newline characters are not attached to the string. It doesn’t change over newline character (\n) into blend of carriage – return (\r) and new-line character (\n).
Function Declaration:
int cputs (const char *str);
Example:
int main(void)
{ c
puts(“Hello MOM”);
getch();
return 0;}
Output:
Hello MOM
cscanf():
The cscanf() work outputs and peruses input from the control center. A configuration specifier is passed to cscanf() capacity to peruse input in the necessary organization. This capacity returns EOF when it came to end of the info stream.
Function Declaration:
int cscanf(char *format, …. );
Note: See the use of cscanf() function in the below example cprintf() function
cprintf():
cprintf() function prints output values in the console as per format speficier.
Function Declaration:
int cprintf(const char *format, …. );
Example:
int main(void)
{
char string[20];
cprintf(“Enter a string value: ”);
cscanf(“%s”,string);
cprintf(“Entered string value is: %s”, string);
return 0;
}
Output:
Enter a string value: TechSupportWhaleEntered string value is: TechSupportWhale
kbhit():
Utilize this capacity to decide if a key has been squeezed or not. It returns a non-zero worth when a key has been squeezed in any case brings zero back.
Function Declaration:
int kbhit();
Example:
int main()
{
while (!kbhit())
printf(“Please press a key\n”);
return 0;}
Output:
This program will keep printing “Please press a key” until user presses any key.
textcolor():
Use this function to change the color of a text.
Function Declaration:
void textcolor(int color);
Example:
int main()
{
textcolor(BLUE);
cprintf(“Welcome to Tech “);
getch();
return 0;}
Output:
Welcome to Tech
textbackground():
Use this function to change background color of the text.
Function Declaration:
void textbackground(<color>);
Example:
int main()
{
textbackground(RED);
cprintf(“Welcome to Tech Support Whale”);
getch();
return 0;}
Output:
Welcome to Tech Support Whale
How to include conio.h in your c program?
You use the following syntax to include the conio header file in your program.
include<conio.h>
What is #include?
The term include for C/C++ writing computer programs is a pre-processor order which assists with bringing in header records in your program. It additionally teaches the compiler to deal with these header records before arrangement.
Conclusion
That’s it. We hope you got a clear idea about conio.h header file and the functions provided by it. Do share your views in the comment section.