Conio.h in C
×


Conio.h in C

50

conio.h is a header file in C programming language used for console input/output.

 It provides a set of functions for performing various operations such as getting a character from the console, clearing the console screen, changing text color, and more.

 While conio.h is not a standard header file and is not supported by all compilers, it is commonly used in many DOS and Windows-based environments for quick and simple console-based applications.

conio.h Functions in C

1 int getch(void): This function reads a character from the console without echoing it to the screen. It is often used for implementing menus and handling interactive input.

2 int getche(void): Similar to getch(), this function reads a character from the console but echoes it to the screen.

3 void clrscr(void): This function clears the console screen, making it useful for creating a clean user interface.

4 void textcolor(int color): This function changes the text color displayed on the console. The color parameter specifies the desired color, which can be one of the predefined color constants provided by conio.h.

5 void gotoxy(int x, int y): This function moves the cursor to the specified position on the console screen, where (0,0) represents the top-left corner.

Program 1: Using getch()

// program for using getch() in C
#include<stdio.h> 
#include<conio.h> 

int main() {
    char ch;
    
    printf("Press any key (without echo)...\n");
    ch = getch();
    
    printf("You pressed: %c\n", ch);
    
    return 0;
}	

Output:

Press any key (without echo)...
(Once a key is pressed)
You pressed: (R)
Program 2: Using getche()

// program for using getche() in C
#include<stdio.h>
#include<conio.h> 

int main() {
    char ch;
    
    printf("Press any key (with echo)...\n");
    ch = getche();
    
    printf("\nYou pressed: %c\n", ch);
    
    return 0;
}	

Output:

Press any key (with echo)...
(Once a key is pressed)
You pressed: (the pressed K)

Program 3: Using clrscr()

// program for using clrscr() 
#include<stdio.h> 
#include<conio.h> 

int main() {
    printf("This text will be cleared after 3 seconds...\n");
    sleep(3); // Wait for 3 seconds
    clrscr(); // Clear the screen
    printf("Console screen cleared.\n");
    
    return 0;
}	

Output:

This text will be cleared after 3 seconds...
(After 3 seconds, the screen clears)
Console screen cleared.
Program 4: Using textcolor()

// program for using textcolor()
#include<stdio.h> 
#include<conio.h> 

int main() {
    printf("This text will be displayed in RED...\n");
    textcolor(RED); // Change text color to RED
    printf("Text color changed.\n");
    
    return 0;
}	

Output:

This text will be displayed in RED...
(Displays text in red color)
Text color changed.

Program 5: Using gotoxy()

// program for using gotoxy() in C
#include<stdio.h> 
#include<conio.h> 

int main() {
    printf("Moving cursor to position (5, 5)...\n");
    gotoxy(5, 5); // Move cursor to position (5, 5)
    printf("Cursor moved.\n");
    
    return 0;
}	

Output:

Moving cursor to position (5, 5)...
(Cursor moves to position (5, 5))
Cursor moved.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments