What Is conio.h In C Programming?

conio.h is a header file used for functions related to console input/output. conio.h has many inbuilt library functions that are used to perform input and output from a c program. Most C programs use this header file.

The functions are declared in conio.h depends on the compiler. As there exists more than one compiler each compiler has few functions declared in conio.h that differs from other compilers. Conio.h header file that comes with Borland compiler of turbo C video RAM to perform Output functions. Few of the functions declared in conio.h are taken from Pascal.

Following are the functions that are declared in conio.h:

_getch

Avoids echo and takes characters directly from the console.  

_getche

Echo character that is taken from the console.

_putch

Place the character on the console.

_cgets

Takes string input from the console.

_cprintf

It formats strings and displays strings on the console.

_cputs

Display the string on the console.

_cscanf

Takes value from the console and formats it.

Description of the above function of conio.h is given below:

_getch

Header file

conio.h

Prototype in conio.h

int _getch(void);

Description in conio.h

_getch takes a character from standard input. Input taken is not buffered and getch() returns the character and does not wait for carriage return.

Other Name of the Function

getch( )

Return Value of the Function

This function will return the next value of character read from the standard input without returning an error.


/* Example for _getch */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
  int input;

  printf(" Please enter character ...");
  input = _getch();
  printf("\n'%c' character is returned by function _getch()\n",input);
  return 0;
}

Output:

_getche

Header file

conio.h

stdio.h

Prototype of the function

int _getche(void);

Description of the function

The _getche( )  function returns the character being read.

Another name of the function

Function: getche

Return Value of the function

This function returns the next standard input character read without displaying error.

/* Example for _getche */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
  int input;

  printf("Press character key from the keyboard : ");
  input = getche();
  printf("\n'%c' character is returned by _getche()\n", input);
  return 0;
}

Output:

_putch

Header file of the function

conio.h

Prototype

int _putch(int c);

Description of the function

This function displays characters from the console and avoids buffering.

Another Name of the Function

putch

Return Value

If the execution is successful then it returns c otherwise it will return EOF.

Example Code

/* Example of _putch */

#include <stdio.h>
#include <conio.h>

int main()
{
  char ch = 0;
  printf(" Please input string: ");
  while ((ch != '\r'))
  {
     ch = getch();
     putch(ch);
  }
  return 0;
}

Output:

_cgets

Header File

conio.h

Prototype Function

char *_cgets(char *buffer);

Description of function

This function takes input from the standard input that is console and saves that character array into the buffer. The element at buffer[0] has the maximum length of the input string which will be read. The element at buffer[1] contains the actual string length. The string is read till carriage-return/line-feed is met.

Return Value

This function returns a pointer which points at buffer[2], this returns no error.

Example Code

/* Example for _cgets
   Also demonstrates _cprintf

    _CGETS.C
*/

#include <conio.h>

int main()
{
   char buffer[22], *return_value;

   buffer[0] = sizeof(buffer) - 2;
   printf("Please enter character strings: ");
   return_value = cgets(buffer);
   printf("\n\rsize of input string is = %d\n\rbuffer = '%s'\n\rreturn value = '%s'\n\r",
      buffer[1],
      buffer + 2,
      return_value);
   return 0;
}

Output:

_cprintf

Header File

conio.h

Prototype of function

int _cprintf(const char *format, arg0,…,argn)

Description of Function

This function displays output on the standard output that is the console. This function uses a pitch function to display characters on the display screen. In addition, this function displays the formatted output.

Return Value of the Function

A return value of this function is characters to be printed on the screen.

Example Code

#include <conio.h>

int main()
{
  cprintf("1. \\n This is the use of cprintf function for I/O, it goes down:\n");
  getch();
  cprintf("2. \\r  yes, it goes back:\r");
  getch();
  cprintf("3. \\r\\n yes. it will go down and back:\r\n");
  getch();
  cprintf("4. Ofcourse, Like this.");
  return 0;
}

Output:

_cputs

Header File

conio.h

Prototype of _cputs

int _cputs(const char *string)

Description of the function

The _cputs function displays the input string on the console.

Return Value of the Function

If the function returns 0 then it is executed successfully otherwise it is executed unsuccessfully.

Example Code

/* Example to display the use of  _cputs.c */

#include <conio.h>

int main(int argc, char *argv[])
{
   se_of_int i;

   for (i = 1; i < argc; i += 1)
   {
      cputs(argv[i]);
      cputs(" ");
   }
   return 0;
}

_cscanf

 Header File

conio.h

Prototype of the Function

Int _cscanf(char *format, arg0,…, argn);

Description of the Function

The cscanf function takes input from the user using the console. This function contains format arguments that specifies the conversion mechanism of the input. The format use to convert the given input is specified in the fscanf function.

Return Value of the Function  

This function returns the converted fileds.

Example Code

/* Example code to explain the use of  _cscanf and _cprintf
*/

#include <conio.h>

int main()
{
  int a_number;
  char a_string[20];

  cprintf("Enter a integer number, then input value of string\n\r");
  cscanf("%d %s", &a_number, a_string);
  cprintf("\nThe input number is %d, the value of string is  '%s'\n",
       a_number, a_string);
  return 0;
}

Output:

Conio.h header file in Turbo C compiler is like this:

/*  conio.h

    Direct MSDOS console input/output.

    Copyright (c) 1987, 1991 by Borland International
    All Rights Reserved.
*/

#if !defined(__CONIO_H)
#define __CONIO_H

#if !defined(__DEFS_H)
#include <_defs.h>
#endif

#define _NOCURSOR      0
#define _SOLIDCURSOR   1
#define _NORMALCURSOR  2

struct text_info {
    unsigned char winleft;
    unsigned char wintop;
    unsigned char winright;
    unsigned char winbottom;
    unsigned char attribute;
    unsigned char normattr;
    unsigned char currmode;
    unsigned char screenheight;
    unsigned char screenwidth;
    unsigned char curx;
    unsigned char cury;
};

enum text_modes { LASTMODE=-1, BW40=0, C40, BW80, C80, MONO=7, C4350=64 };

#if !defined(__COLORS)
#define __COLORS

enum COLORS {
    BLACK,          /* dark colors */
    BLUE,
    GREEN,
    CYAN,
    RED,
    MAGENTA,
    BROWN,
    LIGHTGRAY,
    DARKGRAY,       /* light colors */
    LIGHTBLUE,
    LIGHTGREEN,
    LIGHTCYAN,
    LIGHTRED,
    LIGHTMAGENTA,
    YELLOW,
    WHITE
};
#endif

#define BLINK       128 /* blink bit */

extern   int _Cdecl directvideo;
extern   int _Cdecl _wscroll;

#ifdef __cplusplus
extern "C" {
#endif

void        _Cdecl clreol( void );
void        _Cdecl clrscr( void );
void        _Cdecl gotoxy( int __x, int __y );
int         _Cdecl wherex( void );
int         _Cdecl wherey( void );
int         _Cdecl getch( void );
int         _Cdecl getche( void );
int         _Cdecl kbhit( void );
int         _Cdecl putch( int __c );

#ifndef _PORT_DEFS
int         _Cdecl inp( unsigned __portid );
unsigned    _Cdecl inpw( unsigned __portid );
int         _Cdecl outp( unsigned __portid, int __value );
unsigned    _Cdecl outpw( unsigned __portid, unsigned __value );
unsigned char _Cdecl inportb( int __portid );
void        _Cdecl outportb( int __portid, unsigned char __value );
#endif  /* !_PORT_DEFS */

int         _Cdecl inport( int __portid );
void        _Cdecl outport( int __portid, int __value );

void        _Cdecl delline( void );
int         _Cdecl gettext( int __left, int __top,
                            int __right, int __bottom,
                            void *__destin);
void        _Cdecl gettextinfo (struct text_info *__r );
void        _Cdecl highvideo( void );
void        _Cdecl insline( void );
void        _Cdecl lowvideo( void );
int         _Cdecl movetext( int __left, int __top,
                             int __right, int __bottom,
                             int __destleft, int __desttop );
void        _Cdecl normvideo( void );
int         _Cdecl puttext( int __left, int __top,
                            int __right, int __bottom,
                            void *__source );
void        _Cdecl textattr( int __newattr );
void        _Cdecl textbackground( int __newcolor );
void        _Cdecl textcolor( int __newcolor );
void        _Cdecl textmode( int __newmode );
void        _Cdecl window( int __left, int __top, int __right, int __bottom);

void        _Cdecl _setcursortype( int __cur_t );
char * _Cdecl cgets( char *__str );
int         _Cdecl cprintf( const char *__format, ... );
int         _Cdecl cputs( const char *__str );
int         _Cdecl cscanf( const char *__format, ... );
char * _Cdecl getpass( const char *__prompt );
int         _Cdecl ungetch( int __ch );

#ifndef _PORT_DEFS
#define _PORT_DEFS

    /* These are in-line functions.  These prototypes just clean up
       some syntax checks and code generation.
     */
unsigned char _Cdecl    __inportb__( int __portid );
unsigned int _Cdecl     __inportw__( int __portid );
void        _Cdecl      __outportb__( int __portid, unsigned char __value );
void        _Cdecl      __outportw__( int __portid, unsigned int __value );

#define inportb         __inportb__
#define inportw         __inportw__
#define outportb        __outportb__
#define outportw        __outportw__

#define inp( portid )      __inportb__( portid )
#define outp( portid,v )  (__outportb__( portid,v ), (int)_AL)
#define inpw( portid )     __inportw__( portid )
#define outpw( portid,v ) (__outportw__( portid,v ), (unsigned)_AX)

#endif  /* _PORT_DEFS */

#ifdef __cplusplus
}
#endif

#endif  /* __CONIO_H */

Other functions of conio.h is as follows:

gotoxy

Syntax

#include <conio.h>

void gotoxy(int x, int y);

Description of the function

It is used to place the cursor at the desired position. The position of the cursor is decided by coordinate x and y. The coordinates at (1,1) point to the upper-most-left corner of the displayed window.

Example Code

//Example code to explain the use of gotoxy function

#include <stdio.h>
#include <conio.h>

int main()
{
   int x, y;

   x = 5;
   y = 10;

   gotoxy(x, y);

   printf("This cursor is at %d - x coordinates and %d - y coordinates.", x, y );

   getch();
   return 0;
}

Output:

highvideo

Syntax

#include <conio.h>

void highvideo(video);

Description of the Function

This code causes the display of character string in bright on the display monitor/console.

Example Code

#include <conio.h>
 
int main(void)
{
    clrscr();
    cprintf("This is not displayed in high intensity\n");se
    highvideo();
    cprintf("This is displayed in high intensity\n");
    
    return 0;
}

Output:

movetext

Syntax

#include

int movetext(int _left, int _top, int _right, int _bottom, int _destleft, int _desttop);

Description of the Function

This function moves text displayed on the screen.

Return Value of the Function

This function returns 1 on success and 0 when no error occurs.

Example Code

#include <conio.h>
#include <stdio.h>

int main(void)
{
clrscr();

printf("This text will move from here\n");
printf("Press any key from the keyboard to move text");

getch();
/*parameters denote coordinate of left,top,right,bottom,destleft,desttop*/
movetext(1,1,30,1,1,20);

return 0;

}

Output:

textbackground

Syntax

#include <conio.h>

void textbackground(int _color);

 

Description of the Function

Change the text background as desired by the programmer.

 

Color codes are taken from enum as defined in conio.h.

enum COLORS {

        BLACK,          /* dark colors */

    BLUE,

    GREEN,

    CYAN,

    RED,

    MAGENTA,

    BROWN,

    LIGHTGRAY,

    DARKGRAY,       /* light colors */

    LIGHTBLUE,

    LIGHTGREEN,

    LIGHTCYAN,

    LIGHTRED,

    LIGHTMAGENTA,

    YELLOW,

    WHITE                              };

Example Code

#include<stdio.h>
#include<conio.h>
 
int main( void )
{
   textbackground(RED);
 
   cprintf("ths answer is wrong with a red background.");
 
   getch();
   return 0;
}

Output:

textcolor

Syntax

#include 

void textcolor(int _color);





Description of the Function

Changes the forecolor of the text.




Text color is taken from following enum.

enum COLORS {

        BLACK,          /* dark colors */

    BLUE,

    GREEN,

    CYAN,

    RED,

    MAGENTA,

    BROWN,

    LIGHTGRAY,

    DARKGRAY,       /* light colors */

    LIGHTBLUE,

    LIGHTGREEN,

    LIGHTCYAN,

    LIGHTRED,

    LIGHTMAGENTA,

    YELLOW,

    WHITE

                                        };
#include<stdio.h>
#include<conio.h>
 
int main( void )
{
   textcolor(YELLOW);
   cprintf("text color of this text is Yellow");
   getch();
   return 0;
}

Output:

wherex

Syntax

#include<conio.h>

int where(void);

Return Value of wherex

This function returns the column where the cursor is at. 1 is the leftmost column.

Example Code

#include<stdio.h>
#include<conio.h>
 
int main( void )
{
   int x;
   printf("The answer of this question is correct\n");
   x = wherex();
   printf("cursor at horizontal position is = %d\n",x);
   return 0;
}

Ouput:

getpass

Syntax

char *getpass (const char *ptr);

Description of the Function

This function takes input from the user and input given the user is not displayed on the screen.

Example Code

#include <conio.h>
 
int main( void )
{
    char *password;
 
    password = getpass("Please enter password: ");
    cprintf("Entered password is : %s\n", password);
    return 0;
}

Output: