Everything about conio.h library functions in C/C++

Whenever you write any C program, you must be using stdio.h and conio.h header files to your program. Have you wondered what these header files are required in your program?

There is a lot of information available about stdio.h header file and its functions on the internet but programmers often struggle to get information about the conio.h header file and its functions.

So in this article, you will find all the conio.h library functions, sample source code, and example of conio.h header file.
Let’s start with the overview.

conio.h C++ Library

What is conio.h in C/C++:

The conio stands for Console-Input-Output. The conio.h is a non-standard header file used in C and C++ programming. This file contains console input-output functions which are mostly used by MS-DOS compilers.

Here we have explained some of the important and most widely used functions of conio.h header file. Click on each function to navigate through each function.

Note: Use Turbo C compiler to compile and execute conio.h header file functions.

Table of content:

conio.h header file functions:

clrscr():

Using this function you can clear the output command window. On the command prompt, we usually print code execution status, error information, and output results. During the code execution, if you want to clear the existing printed information on the output console then you can use clrscr() function.

Function Declaration:

void clrscr(void);

Example:

int main()
{
    // Message to be cleared from the screen
    printf("Press any key to clear the screen");
    getch();
    clrscr();
    // Message on the screen after clearing the previous message
    printf("The previous screen is now cleared.\n");
    getch();
    return 0;
}

Read also: 50 most commonly used Turbo C++ keyboard shortcuts

getch():

Use this function to read characters from the keyboard. This function is also used to hold the output screen until the user enters any character. If you don’t use this function then the output screen closes within a fraction of a second.

The getch() is a non-standard function provided by conio.h whereas getchar() is a standard c library function.

Function Declaration:

int getch(void);

Example:

int main()
{
    printf("Press any key to continue…");
    getch();
    return 0;
}

getche():

This function is similar to getch() function. The only difference is that this function also prints the value entered by the user in the output window.

Function Declaration:

char getche();

Example:

int main()
{
    printf("Do you want to proceed? Y or N”)
    getche();
    return 0;
}

Output:

Do you want to proceed? Y or N
Y

putch():

Use this function to print information in the output window. This function prints only one character at a time on output window.

Function Declaration:

int putch (int c);

Example:

int main()
{
    char ch =’w’;
    putch(ch);
}

Output:

w

cgets():

Use this function to read a string of characters from the console. This function reads characters until it encounters carriage-return (CR) and linefeed (LF). The cgets() function replaces CR/LF with the null terminator (\0) at the end of the string.

Function Declaration:

char* cgets(char* str);

Example:

int main(void)
 {
    char buffer[83];
    char *p;
    buffer[0] = 81;
    printf("Enter some characters:");
    p = cgets(buffer);
    printf("\n Entered characters: \"%s\"\n",p);
    return 0;
 }

cputs():

Use this function to print a string of characters on the output screen. The carriage-return and newline characters are not appended to the string. It does not convert newline character (\n) into combination of carriage – return (\r) and new-line character (\n).

Function Declaration:

int cputs (const char *str);

Example:

int main(void)
{
    cputs(“Hello Friends”);
    getch();
    return 0;
}

Output:

Hello Friends

cscanf():

The cscanf() function scans and reads input from the console. A format specifier is passed to cscanf() function to read input in the required format. This function returns EOF when it reached to end of the input stream.

Function Declaration:

int cscanf(char *format, …. );

Note: See the use of cscanf() function in the below example cprintf() function.

cprintf():

cprintf() function prints output values in the console as per format speficier.

Function Declaration:

int cprintf(const char *format, …. );

Example:

int main(void)
{
    char string[20];
    cprintf(“Enter a string value: ”);
    cscanf(“%s”,string);
    cprintf(“Entered string value is: %s”, string);
    return 0;
}

Output:

Enter a string value: TechSupportWhale
Entered string value is: TechSupportWhale

kbhit():

Use this function to determine whether a key has been pressed or not. It returns a non-zero value when a key has been pressed otherwise returns zero.

Function Declaration:

int kbhit();

Example:

int main()
{
    while (!kbhit())
    printf("Please press a key\n"); 
    return 0;
}

Output:

This program will keep printing “Please press a key” until user presses any key.

textcolor():

Use this function to change the color of a text.

Function Declaration:

void textcolor(int color);

Example:

int main()
{
    textcolor(BLUE);
    cprintf("Welcome to Tech Support Whale");
    getch();
    return 0;
}

Output:

Welcome to Tech Support Whale

textbackground():

Use this function to change background color of the text.

Function Declaration:

void textbackground(<color>);

Example:

int main()
{
    textbackground(RED);
    cprintf("Welcome to Tech Support Whale");
    getch();
    return 0;
}

Output:

Welcome to Tech Support Whale

How to include conio.h in your c program?

You can use the following syntax to include the conio header file in your program.

include<conio.h>

What is #include?

The term ‘include’ in C/C++ programming is a pre-processor directive which helps to import header files in your program. It also instructs the compiler to process these header files before compilation.

Conclusion:

That’s it. We hope you got a clear idea about conio.h header file and the functions provided by it. Do share your views in the comment section.

Happy learning 🙂

Please follow and like us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top