When writing any C code, we must include the stdio.h and conio.h header files. Have you ever thought why the code need these header files?
There is a lot of publicly accessible data regarding the stdio.h header file and its functions, but programmers frequently have trouble locating information on the conio.h header file and its operations.
Hence, in this post, students will find all of the conio.h library functions, example source code, and a conio.h header file instance.
Let us begin with an introduction.
In C/C++, what is conio.h?
Conio is an abbreviation for Console-Input-Output. The conio.h header file is a non-standard header file that is used in C and C++ code. This file contains console input/output functions used primarily by MS-DOS compilers.
We've gone through a few of the most significant and often used functions in the conio.h header file.
Contents:
- clrscr
- getch
- getche
- putch
- cgets
- cputs
- cscanf
- cprintf
- kbhit
- textcolor
- textbackground
Functions in the conio.h header file:
1. clrscr:
Function Declaration
void clrscr(void);
We can erase the output command window with this operation. We commonly display the code execution progress, mistake of information, and expected outputs from the command prompt. In order to clear the previous written information on the output console during code execution, use the clrscr function.
Code:
int main()
{
// The message will be removed from the screen
printf("To clear the screen, enter any key");
getch();
clrscr();
// Message displayed on the screen after the preceding message has been cleared
printf("The previous screen has now been removed.\n");
getch();
return 0;
}
1. getch:
Function Declaration
int getch(void);
This operation can be used to retrieve characters from the keyboard. This technique additionally serves to keep the output screen shown until the user inputs a character. If this technique is not used, the output screen disappears in a fraction of a second.
Conio.h has a non-standard function called getch, although getchar is a standard c library function.
Code:
int main()
{
printf("To continue, simply tap any key.?");
getch();
return 0;
}
3. getche:
Function Declaration
char getche();
getche is a method that is similarly to getch. Just one distinction is that in the output window, this function additionally prints the value entered by the user.
Code:
int main()
{
printf("Do you wish to continue? Y or N")
getche();
return 0;
}
Output
Do you wish to continue?? Y or N
Y
4. cgets:
Function Declaration
char* cgets(char* str);
Read a string of characters from the console using this function. This method reads characters until it comes across carriage-return (CR) and linefeed characters (LF). At the conclusion of the string, the cgets function replaces CR/LF with the null terminator (0).
Code:
int main(void)
{
char buffer[83];
char *p;
buffer[0] = 81;
printf("Input some characters:");
p = cgets(buffer);
printf("\n Inputted characters: \"%s\"\n",p);
return 0;
}
5. cputs
Function Declaration
int cputs (const char *str);
Use cputs to display a character string to the output screen. The carriage-return and newline characters are removed from the string. It is unable to change newline character (n) to carriage return (r) and new-line character (n) combination.
Code:
int main(void)
{
cputs("Hi Folks");
getch();
return 0;
}
Output
Hi Folks
6. cscanf:
Function Declaration
int cscanf(char *format, …. );
The cscanf function scans and analyzes console input. To read input in the desired format, a format specifier is supplied to the cscanf method. When it reaches the end of the input stream, this method returns EOF.
Note: The cscanf function is used in the instance cprintf code below.
2. cprintf:
Function Declaration
int cprintf(const char *format, …. );
The cprintf function prints output values to the console based on the format specification.
Code:
int main(void)
{
char string[20];
cprintf("Input a string value: ");
cscanf("%s",string);
cprintf("Inputted string value is: %s", string);
return 0;
}
Output
Input a string value: JavaCppTutorial
Inputted string value is: JavaCppTutorial
8. kbhit:
Function Declaration
int kbhit();
kbhit: When a key is pushed, it returns a non-zero value; else, it returns zero.
Code:
int main()
{
while (!kbhit())
printf("Pls enter a key. \n");
return 0;
}
Output
This code will continue to print "Pls enter a key" until the user enters a key.
9. textcolor:
Function Declaration
void textcolor(int color);
This function is used to change the colour of text.
Code:
int main()
{
textcolor(RED);
cprintf("Welcome to JavaCppTutorial");
getch();
return 0;
}
Output
Welcome to JavaCppTutorial
10. extbackground:
Function Declaration
void textbackground(<color>);
This function is used to modify the colour of the text's background.
Code:
int main()
{
textbackground(BLUE);
cprintf("Welcome to JavaCppTutorial");
getch();
return 0;
}
Output
Welcome to JavaCppTutorial
- How should conio.h be included in the Program code?
To add the conio header file in the code, use the following syntax.
#include<conio.h>
- What exactly is #include?
In C/C++ programming, the term 'include' refers to a pre-processor directive that allows programmers to import header files into their code. It also tells the compiler to perform these header files prior to compilation.
Conclusion:
That's all there is to it. We expect students now have a good understanding of the conio.h header file and the functions it provides.