When crafting C code, it is essential to incorporate the stdio.h and conio.h header files. Have you ever pondered the rationale behind the necessity of these header files in the code?
There is a wealth of openly available information about the stdio.h header file and its functions, yet developers often struggle to find details on the conio.h header file and its functionalities.
Therefore, within this article, learners will discover a comprehensive list of all conio.h library functions, an illustration of sample source code, and an example of a conio.h header file.
Let us begin with an introduction.
In C/C++, what is conio.h?
Conio stands for Console Input Output. The conio.h header file is considered non-standard and is commonly employed in C and C++ programming. It includes functions for console input and output, mainly utilized by compilers designed for MS-DOS environments.
We have examined several of the most important and commonly utilized functions within 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 have the ability to clear the output console using this action. It is a standard practice to showcase the progress of code execution, errors, and the anticipated results from the command line interface. To wipe out the previously displayed content on the output console while the code is running, you can employ 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.
The conio.h header provides a non-standard function known as getch, while getchar is a function that is part of the standard C library.
Code:
int main()
{
printf("To continue, simply tap any key.?");
getch();
return 0;
}
3. getche:
Function Declaration
char getche();
getche is a function that operates similarly to getch. The key difference is that getche not only retrieves the input from the user but also displays the entered value in the output window.
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 sequence of characters from the console using the specified function. This function reads characters continuously until it encounters carriage-return (CR) and linefeed (LF) characters. Once the string is complete, the cgets function substitutes 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);
Utilize the cputs function to exhibit a character sequence on the output screen. This function eliminates the carriage return and newline characters from the provided string. Conversion of the newline character ('\n') to a combination of carriage return ('\r') and newline ('\n') is not supported by this function.
Code:
int main(void)
{
cputs("Hi Folks");
getch();
return 0;
}
Output
Hi Folks
6. cscanf:
Function Declaration
int cscanf(char *format, …. );
The cscanf function is responsible for capturing and interpreting user input from the console. In order to extract input in a specific format, a format specifier needs to be provided to the cscanf function. Upon reaching the end of the input stream, this function will signal the end of file by returning 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 displays output values on the console according to the specified format.
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();
The function kbhit returns a non-zero value when a key is pressed, and it returns zero when no key is pressed.
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 employed to modify the color 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 employed to adjust the color of the background of the text.
Code:
int main()
{
textbackground(BLUE);
cprintf("Welcome to JavaCppTutorial");
getch();
return 0;
}
Output
To include conio.h in the program code, you can utilize the following line of code:
#include <conio.h>
To include the conio header file in your code, utilize the syntax provided below.
#include<conio.h>
- What exactly is #include?
In C/C++ programming, the term 'include' denotes a pre-processor directive enabling developers to incorporate header files in their code. It instructs the compiler to process these header files before compilation begins.
Conclusion:
That covers the basics. We anticipate that students now possess a solid comprehension of the conio.h header file and its associated functions.