conio.h in C
Overview
In this article, we will get to know about the very useful header file in C which is known as conio.h. We will also look over the uses of conio.h in C programming language and we will also look over the various functions residing in conio.h.
What is conio.h in C Programming?
It is a header file in C which is most widely used in the C programming language. It stands for console input-output. Many built-in library functions in conio.h are utilised by c programmes for input and output. The functions which are declared inside the conio.h varies from compiler to compiler.
Note: GCC compilers do not support conio.h header file.
Here, we will see all the programs compiled based on the Turbo C compiler.
Why do we Use conio.h in C ?
conio.h is a header file in which there are many built-in functions embedded in it they generally perform input/output on the console i.e., it is used to take input from the keyboard given by the user and display output on the screen. Some functions of conio.h such as getch() is used to hold the screen till the user has pressed any key.
Function Declared in conio.h in C
There are various functions inside the conio.h header files some of which are:
- clrscr()
This function is used to clear the output on the screen.
Code :
Output :
Explanation :
Here, in the above code, we have included two header files stdio.h and conio.h then we have printed 2 statements and used the clrscr() which is then followed by a print statement. So the clrscr() function clears all the statements above it.
- getch()
One character is read off the keyboard. It avoids echo of character. We want the window to wait for user input before continuing or terminating the program.
Code :
Output :
Explanation :
Here, in the above code, we have included the two header files stdio.h and conio.h and then we have printed one line to take input from the keyboard after that we have used the getch() function which will wait until the user doesn't give input.
- getche()
It is kind of similar to getch() but it can accept alphanumeric values. It simultaneously prints the character on the screen which is given as the input that echoes the character.
Code :
Output :
Explanation :
Here, in the above code, we have included two header files stdio.h and conio.h and then we have printed one line which prints the Input by the user and then we have used getche() which takes alphanumeric character and simultaneously prints the character on the screen or console.
- putch()
It prints or puts a character on the screen/console.
Code :
Output :
Explanation :
Here, in the above we have performed the same action as in the getch() function which is we have used two header files stdio.h and conio.h then we have stored a character in a string variable and then we have used putch() and passed the variable in it which prints the character on the screen or console.
- cgets()
It takes a group of characters which is a string as input from the console until it gets a carriage-return(CR) and linefeed(LF). A combination of both gives us the new line effect(\n). So, these CR/LF replaces with the null terminator(\O) placed at the end of the string.
Code :
Output :
Explanation :
Here, in the above code, we have included the two header files stdio.h and conio.h then we have made a variable s of size 100 and an str variable to store the input value and then use to print it. After that, we have cgets() to input a string and then we have stored in it a variable s and then at the last statement we have print the value.
- cputs()
It prints all the strings of characters on the output screen or console.
Code :
Output :
Explanation :
Here, in the above code we have included two header files stdio.h and conio.h then we have used cputs to print a string which displays the string on the console.
- cscanf() and cprintf()
They both work the same as like scanf() and printf(). Scanf() take input from the console given by the user and formats it and similarly printf() formats the string and print the formatted string on the console or screen.
Code :
Output :
Explanation :
Here, in the above code we have included two header files stdio.h and conio.h then we have taken a variable of the string data type of size 50 named marks after that we have used cprintf() function to print a line and after that, we have used cscanf() to take input from the user and after that, we have used cprintf() function again to print a string on the console.
- kbhit()
This function helps us to tell whether a user has pressed any key or not. It is used in many games to know whether the user has pressed any key or not.
Code :
Output :
Explanation :
Here, in the above code we have included two header files stdio.h and conio.h after that we have used the kbhit() function in a Do while loop. So, the kbhit() function will run the loop until and unless any key is pressed and if the key is pressed the loop will terminate and the program will end.
- textcolor()
This function is used to modify the colour of the text.
- textbackground()
This function is used to modify the background colour of the text.
- delline()
We use this function to remove one or more lines from the screen.
Code :
Output :
Explanation :
Here, in the above code we have included two header files stdio.h and conio.h and after that, we have printed two lines, we used the delline function which clears a single line just above it and then again we have print one line which will be printed after the execution of the function.
- gotoxy()
This function works in such a way that it accepts two parameters and after that using those two parameters it takes your cursor on the particular coordinate of your window.
Code :
Output :
Explanation :
Here, in the above code, we have included two header files stdio.h and conio.h and after that, we have taken two integer values and passed that we have passed these two values as a parameter to the function gotoxy() and after the execution of the program, the position of the cursor will be changed.
- wherex() and wherey()
This function tells about the current position of the cursor in the X and Y coordinates. The wherex() function tells the x-coordinate of the cursor in the current output screen whereas the wherey() function tells the y-coordinate of the cursor in the current output screen.
Code :
Output :
Explanation :
Here, in the above code we have included two header files stdio.h and conio.h after that we have taken two integers x and y and then we have stored the value of wherex() in x and wherey() in y after that we will print the value of the x and y and it will tell the current x and y coordinates of the cursor on the current screen.
Other Function of conio.h Library
- highvideo()
This function is used to brighten the character string and displays it on the screen.
Code :
Output :
Before using the function
After the function
Explanation :
Here, in the above code we have included two header files stdio.h and conio.h after that we have clear the screen using the clrscr() function we printed one line and then we used the highvideo() function which will increase the intensity of the statement next below it.
- movetext()
This function is used to move the text which is displayed on the screen.
Syntax :
Return value :
If there are no errors, this method returns 0 on failure and 1 on success.
Code :
Output :
Explanation :
Here, in the above code we have included two header files stdio.h and conio.h after that we print one line then we take input from the user to execute the movetext() function because we have used to getch() function. At last, after the key is pressed movetext() function will execute and the position of the text will be changed.
- getpass()
This function is used to take input from the user and the input which is given by the user will not be displayed on the output screen. This can be used where the data you want to enter is private and you don't want to show the text.
Code :
Output :
Explanation :
Here, in the above code we have included two header files stdio.h and conio.h after that we have taken a character variable which is used to take the input given to the getpass() function. The key which we have entered is not displayed on the screen which can be seen above after that, we stored the value entered in the key and in the next line we printed the key value.
Conclusion
- conio.h is a header file in the C programming language.
- conio.h stands for console input - output.
- It has many inbuilt functions which are used to perform many input and output operation
- Some of the builtin function are - clrscr(), getch(), getche(),gotoxy(),wherex(),wherey() and many more.
- Some other functions used in conio.h are - highvideo(), movetext(), and getpass().