Signup/Sign In
LAST UPDATED: APRIL 13, 2023

conio.h Library Functions in C

    Till now, you must have heard a lot about stdio.h header file which is one of the standard header files in C. In this tutorial, we will learn about a non-standard but very useful header file in C, conio.h.

    conio stands for console input-output. It contains console input and output functions mostly used by MS-DOS compilers. The GCC compiler does not support this header file. Here, we will use Turbo C to compile our programs.

    List of Functions in conio.h

    Following are some of the functions of the header file conio.h -

    1. clrscr()
    2. getch()
    3. getche()
    4. putch()
    5. cgets()
    6. cputs()
    7. cscanf()
    8. cprintf()
    9. kbhit()
    10. textcolor()
    11. textbackground()
    12. delline
    13. gotoxy
    14. wherex
    15. wherey

    Let's look at the functions one-by-one.

    1). clrscr() in C

    This function is used to clear the output screen.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        printf("Welcome to studytonight.");
        printf("\nThese two sentences will be cleared.");
        clrscr();
        printf("This appears after clearing the screen.");
        return 0;
    }


    This appears after clearing the screen.
    Press any key to continue.

    2). getch() in C

    The getch in C reads one character from the keyboard. We don’t care about the input here. We want that the window should wait for the user's input to proceed/end the program. Here is an example of the getch function in C -

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        printf("Enter a character: ");
        getch();
        return 0;
    }


    Enter a character:
    Press any key to continue.

    3). getche() in C

    getche() is similar to getch() but it can input alphanumeric values too. It also prints the value of the character we input on the screen.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        printf("Enter a character: ");
        getche();
        return 0;
    }


    Enter a character: a
    Press any key to continue.

    4). putch() in C

    It prints one character on the output screen.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        char s = 'a';
        putch(s);
        return 0;
    }


    a
    Press any key to continue.

    5). cgets() in C

    char* cgets(char* str);

    It reads a string of characters from the console until it encounters carriage-return (CR) and linefeed (LF)(Carriage return points the cursor to the beginning of the line horizontly and Line feed shifts the cursor to the next line vertically. Combination of both gives us new line(\n) effect.). It replaces CR/LF with the null terminator (\0) at the end of the string.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
     {
        char buffer[100];
        char *p;
        buffer[0] = 50;
        printf("Enter some characters:");
        p = cgets(buffer);
        printf("\nEntered characters: %s", p);
        return 0;
     }


    Enter some characters:studytonight
    Entered characters: studytonight
    Press any key to continue.

    6). cputs() in C

    int cputs(const char* str);

    It prints a string of characters on the output screen.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        cputs("Hola amigo!");
        return 0;
    }


    Hola amigo!
    Press any key to continue.

    7). cscanf() and cprintf()

    These work like printf() and scanf(). They work on values as per the format specifier.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        char name[50];
        cprintf("Enter your name: ");
        cscanf("%s", name);
        cprintf("\nHello, %s",name);
        return 0;
    }
    


    Enter your name: abc
    Hello, abc
    Press any key to continue.

    8). kbhit()

    kbhit() tells us if a user has pressed any key or not. It is useful in games to tell whether a user has pressed a key.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        do{
    		printf("Press any key to stop the process.\n");
    	}while(!kbhit());
    	return 0;
    }
    


    Press any key to stop the process.
    Press any key to stop the process.
    Press any key to stop the process.
    Press any key to stop the process.
    Press any key to stop the process.
    Press any key to stop the process.
    Press any key to stop the process.
    Press any key to continue.

    9). textcolor()

    It is used to change the color of the text.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        int i;
        for (i = 1; i <= 10; i++) {
            textcolor(i);
            cprintf("studytonight");
            cprintf("\n\r");
        }
        textcolor(RED);
        cprintf("Bye");
        return 0;
    }

    textcolor() example

    We can also set it as a blinking color as follows-

    textcolor(BLUE + BLINK);

    10). textbackground() in C

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        int i;
        for (i = 1; i <= 10; i++) {
            textbackground(i);
            cprintf("studytonight");
            cprintf("\n\r");
        }
        textbackground(RED);
        cprintf("Bye");
        return 0;
    }
    // Press any key to continue.
    
    

    textbackground() example

    11). delline() in C

    This function is used to clear single or multiple lines from the screen.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        printf("Welcome to studytonight.");
        printf("\nOnly this sentence will be cleared because we are using delline() only once.");
        delline();
        printf("\nThis appears after clearing the screen.");
        return 0;
    }


    Welcome to studytonight.
    This appears after clearing the screen.
    Press any key to continue.

    12). gotoxy() in C

    This function takes two parameters and takes your cursor on a particular coordinate on the window.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        int x = 90, y = 50;
        gotoxy(x, y);
        printf("The position of cursor is changed");
    	return 0;
    }
    
    


    The position of cursor is changed.
    Press any key to continue.

    13). wherex() and wherey()

    They tell us the position of the cursor. wherex() gives us the X-axis co-ordinate while wherey() gives us the Y-axis co-ordinate.

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        int x, y;
        cprintf("studytonight");
        x = wherex();
        y = wherey();
        cprintf("\n\rX and Y coordinates of the cursor are - (%d, %d)", x, y);
        return 0;
    }

    wherex() and wherey() output

    Conclusion

    Conio.h is a non-standard but very useful header file in C. It is mostly used for console input and output functions by MS-DOS compilers. It contains a list of functions, including clrscr(), getch(), getche(), putch(), cgets(), cputs(), cscanf(), cprintf(), kbhit(), textcolor(), textbackground(), delline, gotoxy, wherex, and wherey. We looked at each function and provided examples of how to use them in your programs.

    Although the GCC compiler does not support this header file, we can still use it with Turbo C. Using conio.h can make your programming experience much easier and more efficient.

    Frequently Asked Questions

    1. What is the conio.h library in C?

    The conio.h library is a C library that provides console input/output functions for DOS and Windows platforms, such as getch(), clrscr(), and gotoxy().

    2. What are some commonly used functions in the conio.h library?

    Some commonly used functions in the conio.h library include getch(), which reads a character from the keyboard, and clrscr(), which clears the console screen. Other functions, like textcolor() and cprintf(), can be used to change text color and print formatted output to the console.

    3. How do I use the conio.h library in my C program?

    To use the conio.h library in your C program, including the line #include <conio.h> at the beginning of your code. Note that the library is specific to DOS and Windows platforms and is not supported on other operating systems like Linux or macOS.

    4. Which platforms support the conio.h library?

    Platforms that support the conio.h library are only DOS and Windows. It is not available on other operating systems like Linux or macOS.

    5. What is getch in C?

    The getch() in C is used to get user input in old operating systems as it accepts only one input character at once.

    You May Also Like:

    I am the founder of Studytonight & Fullstack developer (MERN). I like writing content about ReactJS, MERN, JavaScript, Docker, Linux, PHP, Go lang, Cloud, Web development, and general Tech related content. I have 10 years of diverse experience in software development.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS