×

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

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.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 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.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 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.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 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.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.