The header file frequently utilized in the C programming language is known by an abbreviation in relation to console input-output operations. C programs extensively rely on the numerous functions provided within the conio.h library for managing input and output tasks. It's important to note that the specific functions available in the conio.h file can vary between different compilers.
Why is conio.h used in C?
Conio.h serves as a header file containing multiple pre-existing functions that are frequently employed for performing input/output operations within the console, or for obtaining user keyboard inputs and displaying outcomes on the screen. Various routines within conio.h, like getch, are utilized to pause the screen until a key is pressed by the user.
Function Declared in the C file conio.h
The conio.h header files consist of various functions, including:
- clrscr
The display can be reset using this function.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Welcome to Logic Practice");
printf("\nThis is the Second sentence of the program");
clrscr();
printf("Output After using clrscr() function");
return 0;
}
Output
Output After using clrscr() function
????????????????????..
Process executed in 1.11 seconds
Press any key to continue
Explanation:
Here, in the provided code snippet, the clrscr function is employed prior to displaying two statements, with the inclusion of the header files stdio.h and conio.h. As a result, all statements preceding the clrscr function are effectively erased.
- getch
The keyboard reads a single character at a time to avoid echoing characters. The window will remain open until the user inputs something before continuing or closing the application.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Enter your character: ");
getch();
return 0;
}
Output
Enter your character: Press any key to continue
????????????????????????????
Process executed in 1.22 seconds
Explanation:
The code snippet above incorporates the inclusion of the stdio.h and conio.h header files. Following a prompt for user input with a printed line, we have employed the getch function, which will pause execution until the user finishes inputting data.
- getche
It bears some similarities to getch, with the added feature of accepting alphanumeric characters. It performs the dual function of displaying the input character and echoing it on the screen.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Enter your character: ");
getche();
return 0;
}
Output
Enter your character: j Press any key to continue.
????????????????????????????
Process executed in 1.22 seconds
Explanation:
So, within the provided code snippet, the header files stdio.h and conio.h have been imported, a command has been included to show user input, and the getche function has been employed to capture an alphanumeric character and simultaneously display it on the screen or console.
- putch
It outputs a character to the console or screen.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
char str = 'j';
putch(str);
return 0;
}
Output
j Press any key to continue.
????????????????????????????
Process executed in 1.22 seconds
Explanation:
Utilizing the header files stdio.h and conio.h, we initiated by storing a character in a string variable. Following this, we employed putch by passing the variable to it, enabling the character to be displayed on the screen. This mirrors the process carried out by the getch function.
- cgets
It receives a sequence of characters entered through the console until a carriage-return (CR) and linefeed (LF) are encountered. The newline character (\n) is produced by the combination of both. Therefore, the null terminator (\0) is employed at the string's conclusion to substitute these CR/LF characters.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
char s[100];
char *str;
s[0] = 50;
printf("Input the character:");
str = cgets(s);
printf("\nOutput on the screen: %s", str);
return 0;
}
Output
Input the character: Welcome to Logic Practice
Output on the screen: Welcome to Logic Practice
???????????????????????????.
Press any key to continue.
Process executed in 0.11 seconds
Explanation
Here, in the provided code snippet, we've imported the header files stdio.h and conio.h. Following that, we've declared a variable 's' with a capacity of 100 characters and another 'str' variable to hold the input value, which is subsequently displayed. Later, we utilize cgets to obtain a string input that is then assigned to the variable 's'. Finally, the value is output using the
- function.
It showcases each character string on the output screen or terminal.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
cputs("Welcome to Logic Practice");
return 0;
}
Output
Welcome to Logic Practice.
???????????????????????????.
Press any key to continue.
Process executed in 0.11 seconds
Explanation:
Here, within the provided code, we have imported two header files namely stdio.h and conio.h. Subsequently, we employed cputs to output a string, which exhibits the specified text on the console. Similarly, we are also utilizing the functions
- cscanf and cprintf.
Both functions function akin to scanf and printf. The scanf function readies input supplied by the user via the console, while printf performs a similar task by formatting the string before displaying it on the console or screen.
#include<stdio.h>
#include<conio.h>
int main()
{
char marks[50];
cprintf("Enter your marks: ");
cscanf("%s", marks);
cprintf("\nPassed, %s",marks);
return 0;
}
Output
Enter your marks: 80
Passed, 80
???????????????
Press any key to continue
Process executed in 1.11 seconds
Explanation
Here, within the provided code snippet, we've imported two header files: stdio.h and conio.h. Following that, a string variable named marks with a capacity of 50 has been declared. Subsequently, the cprintf function is employed to display a line, the cscanf function is utilized to gather user input, and another cprintf function is used to output a string on the console.
- kbhit
This functionality allows us to check if a user has pressed any keys. It is commonly used in various games to detect player input.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
do{
printf("To stop this loop press any key\n");
}while(!kbhit());
return 0;
}
Output
To stop this loop press any key
To stop this loop press any key
To stop this loop press any key
... Until the key is pressed
??????????????????
Press any key to continue
Process executed in 1.11 seconds
Explanation:
The kbhit function was implemented within a while loop in the previous code, following the inclusion of two header files - stdio.h and conio.h. Consequently, the kbhit function will sustain the loop until a key is pressed, leading to its termination and subsequently ending the program.
- delline
This functionality is employed to remove a single line or multiple lines from the display.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Welcome to Logic Practice");
printf("\n Due to the delline function being used below, this line will be cleared.");
delline();
printf("\nText printed after using delline function.");
return 0;
}
Output
Welcome to Logic Practice
Due to the delline function being used below, this line will be cleared.
Text printed after using delline function.
??????????????????????????.
Press any key to continue
Process executed in 1.11 seconds
Explanation:
Here, within the provided code snippet, we have imported two header files: stdio.h and conio.h. Following this, we have displayed two lines of output, applied the delline function to erase the line directly above it, and subsequently reprinted a single line to demonstrate the outcome of the function's operation.
- gotoxy
The method functions by accepting a pair of arguments and relocates the cursor to a specified position on the window based on these parameters.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int a = 100, b = 50;
gotoxy(a,b);
printf("Cursor Position has been changed.");
return 0;
}
Output
The cursor position has been changed.
...................................................................
Process executed in 1.11 seconds
Press any key to continue.
Explanation:
The code snippet above has imported the stdio.h and conio.h header files, followed by passing two integer values as arguments to the gotoxy function to adjust the cursor position post-execution.
- The functions wherey and wherex
This function supplies details regarding the current X and Y coordinates of the cursor. wherey indicates the vertical position of the cursor on the current output screen, while wherex discloses the horizontal position of the cursor on the current output screen.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y;
x = wherex();
y = wherey();
cprintf("\n\rThe Coordinates of X and Y are - (%d, %d)", x, y);
return 0;
}
Output
The Coordinates of X and Y are- (1,2)
...................................................................
Process executed in 1.11 seconds
Press any key to continue.
Explanation:
The code above incorporated the stdio.h and conio.h header files, followed by the declaration of two integers, x and y. The values of whereX were stored in x, and whereY values were stored in y. Subsequently, the current x and y coordinates of the cursor on the active screen were displayed.