×

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions and macro definitions to be shared between various source files. Header files are used in the program with the C preprocessor directive i.e “#include”.

Here are some of the header files in C language.

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<iostream.h>

#include<iomanip.h>

Conio.h

Conio. h is an abbreviation for console input/output header file, which manages input/output on the console, here console is the screen where output is displayed.

MS-DOC compilersuse it mostly for console input/output.C standard library or ISO C does not contain this header file, also not defined by POSIX.

This conio.h header file declares many useful functions such as shown below. The header file in our program is to be included to use the following functions.In conio.h the declared library functions vary from compiler to compiler

  • kbhit();
  • textColor();
  • textbackground();
  • clrscr();
  • getch();
  • getche();
  • gotoxy();
  • wherex();
  • wherey();

There are mainly two functions i.e getch() and clrscr() which are used with the conio.h apart from these many other functions are also used.

getch();(get a character)

This function is nonstandard.From the keyboard,it reads a single character and doesnot print it on console. It is alsoused to hold the output window until any key is pressed. It is without echo that means does not print on screen.

Example:

#include <stdio.h>
#include <conio.h> //header file
void main()
{
Printf(“hold the screen”);
getch();  //getch function
}

Output

hold the screen

clrscr();   (clear screen)

To clear the previous output in the console window this function is used. The output window will become messy if we won’t use this function.

Example:

#include <stdio.h>
#include <conio.h>
void main()
{
		clrscr();//clrscr function
Printf(“hello javatpoint with clear console”);
getch();
}

Output

hello javatpoint with clear console

kbhit();    (keyboard hit)

This functionis used to check whether a key is pressed or not. It returnsa zero value when a key is not pressed and returnsa non-zero value when a key is pressed. It should be avoided.

Example:

#include <stdio.h>
#include <conio.h>
int main()
{
do {
printf("stop loop by entering any key");
} 
while (!kbhit());//getch function
return 0;
}

Output

stop loop by entering any key
stop loop by entering any key
stop loop by entering any key
stop loop by entering any key
stop loop by entering any key
Till N time, until you don't press any key.

textcolor();

The color of the text can be changed by using this textcolor( ) function. It is mostly not supported.Here we directly color the name or integer value from 0 to 15. Use the colors in the upper case.

BLACK0

BLUE1

GREEN2

CYAN 3

RED4

MAGENTA5

BROWN6

LIGHT GRAY7

DARK GRAY8

LIGHT BLUE 9

LIGHT GREEN10

LIGHT CYAN11

LIGHT RED12

LIGHT MAGENTA13

YELLOW 14

WHITE15

Example:

#include<stdio.h>
#include<conio.h>
Void Main(){
textcolor(RED);// textcolor function 
cprintf("C");
getch();
return 0
}

Output

C

Note: cprintf function is used in place of printf. Because the formatted output to the text window on the screen is sent by cprintf where printf sends it to stdin.

textbackground();

The background color in text mode can be changed by using this textbackground(). It is also not supported mostly. Use the color name in the upper case.

Example:

include<stdio.h>
#include<conio.h>
main()
{
textbackground(YELLOW); // textbackground function
cprintf("change background color");
getch();
return 0;
}

Output

change background color

getche();   (get character with echo)

From the keyboard, it takes the input and immediately it displays without waiting until enter key is pressed.It is with an echo which means itprintsa character on screen.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf(“Press any character: ”);
ch = getche();// getche function
printf(“\nPressed character is: %c”, ch);
}

Output

Press any character: e
Pressed character is: e

wherex();

Used for getting cursor position. The co-ordinate of x is given by the wherex() function. Mainly used in game making.

Example:

#include <stdio.h>
#include <conio.h>
main()
{
int a;
printf("Includehelp\n\n");
a = wherex();// printing the position of cursor
printf("The cursor position on X-axis after message printing = %d\n",a);
getch();
return 0;
}

Output:

Includehelp
The cursor position on X-axis after message printing =1

The cursor position on X-axis after message printing =1

wherex();

Used for getting cursor position. The co-ordinate of y is given by the wherex() function. Mainly used in game making.

Example:

#include <stdio.h>
#include <conio.h>
main()
{
int a;
printf("Includehelp\n\n");
a = wherey();   // printing the position of cursor
printf("The cursor position on Y-axis after message printing = %d\n",a);
getch();
return 0;
}

Output:

Includehelp
The cursor position on Y-axis after message printing =1

Important Points

  • In turbo C/C++ we find the conio.h header file that uses a 16-bit compiler. It is not used in the GCC compilers because, functions like getch(),clrscr() etc, are not available to use.
  • In GCC compiler clear of the console is done automatically and no need to worry about getch() functions.

Related Topics

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.