www.digitalmars.com [Home] [Search] [CTG] [RTL] [IDDE]

Last update Sep 14, 2002


conio.h

Console versions of common I/O functions. They bypass the stdin and stdout buffers and access the console directly.

Function Description
_kbhit Determines if a keyboard key was pressed.
_ungetch Puts a character back into the keyboard buffer.
_getch Reads a character directly from the console, without echo.
_getche Reads a character directly from the console, with echo.
_putch Writes a character directly to the console.
_cgets Gets a string directly from the console.
_cprintf Formats and prints a string directly to the console.
_cputs Outputs a string directly to the console.
_cscanf Reads and formats values directly from the console.


_kbhit

Header

conio.h
stdio.h

Prototype

int _kbhit(void);
int kbhit(void);

Description

Checks if a keyboard key has been pressed but not yet read.

Return Value

Returns a non-zero value if a key was pressed. Otherwise, returns 0.

Compatibility

DOS, Windows 3.x, Phar Lap, DOSX, Win32

See Also

_getch
_getche

Example

/* Example of _kbhit() */ 

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

int main() 
{   
   printf("Hit any character key when ready\n"); 
   while (!_kbhit())
	;
   printf("\nThe key pressed was (%c)\n", _getch()); 
   return 0;
}
Output

Hit any character key when ready
	 
The key pressed was (b) 

_ungetch

Header

conio.h

Prototype

int _ungetch(int c);
int ungetch(int c);

Description

_ungetch function puts the character c back into keyboard buffer, so that c will be the next character read by _getch or _getche. _ungetch can be called only once before the next read.

Return Value

Returns c if successful. If unsuccessful, EOF is returned.

Compatibility

DOS, Windows 3.x, Phar Lap, DOSX, Win32

See Also

_getch
_getche
ungetc

Example

/* Example for _ungetch */

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

int main() 
{   
   int i = 0; 
   char c; 
   char str[128]; 
   printf("Enter an integer: ");
   while ((c = _getche()) != EOF && isdigit(c)) 
      i = (i * 10) + (c - 48); 

   if ((c != EOF) && (c != '\r')) 
      ungetch(c); 

   if (c != '\r') 
   {   
      gets(str);
      printf("The integer is %d, and the rest is \"%s\"\n", i, str); 
   } 
   else 
      printf("\nThe integer is %d", i); 
   return 0;
}
Output

Enter an integer: 100 bottles of beer on the wall
The integer is 100, and the rest is "bottles of beer on the wall"

_getch

Header

conio.h
stdio.h

Prototype

int _getch(void);

Description

_getch obtains a character from stdin. Input is unbuffered, and this routine will return as soon as a character is available without waiting for a carriage return. The character is not echoed to stdout. _getch bypasses the normal buffering done by getchar and getc. ungetc cannot be used with _getch.

Synonym

Function: getch

Return Value

Returns the next character read and extended character codes, but never returns an error.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

getc
getchar
_getche
putchar

Example


	/* 	Example for _getch */ 

	#include <stdio.h>
	#include <stdlib.h>

	int main() 
	{ 
	   int input; 

	   printf(" Press charcter key..."); 
	   input = _getch(); 
	   printf("\n'%c' was returned by _getch()\n", 
		  input); 
	   return 0;
	} 
Output

Press charcter key...
'g' was returned by _getch()

_getche

Header

conio.h
stdio.h

Prototype

int _getche(void);

Description

The _getche function is the same as _getch except that the character being read is echoed to stdout. Like _getch, it bypass normal buffering done by getchar and getc. ungetc cannot be used with _getche.

Synonym

Function: getche

Return Value

Returns the next character read but never returns an error.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

getc
getchar
_getch
getchar
putc
stdout

Example

	/* 	Example for _getche */ 

	#include <stdio.h>
	#include <stdlib.h> 

	int main() 
	{ 
	   int input; 

	   printf(" Input a character: "); 
	   input = _getche(); 
	   printf("\n'%c' was returned by _getche()\n", 
		  input); 
	   return 0;
	} 
Output

Input a character: g
'g' was returned by _getche()

_putch

Header

conio.h

Prototype

int _putch(int c);

Description

The _putch function writes character c to the console, without buffering.

Synonym

Function: putch

Return Value

If successful, returns c. Otherwise, returns EOF.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

_cprintf
_getch
_getche

Example


	/* 	Example of _putch */ 

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

	int main()
	{ 
	   char ch = 0; 
	   printf(" Input a string: ");
	   while ((ch != '\r')) 
	   {
	      ch = _getch(); 
	      _putch(ch);
	   } 
	   return 0;
	} 
Output

Input a string: The quick brown fox jumped over the lazy dog.

_cgets

Header

conio.h

Prototype

char *_cgets(char *buffer);

Description

The _cgets function gets a character string from the console and stores it in the character array pointed to by buffer. The array's first element, buffer[0], must contain the maximum length, in characters, of the string to be read. The array's second element, buffer[1], is where _cgets stores the string's actual length _cgets reads characters until it reads the carriage-return/ line-feed combination or the specifed maximum number of characters.

Return Value

A pointer to the start of the string at buffer[2]. Returns no error.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

_getch
_getche
_cprintf

Example


	/* 	Example for _cgets 
		Also demonstrates _cprintf 

	    _CGETS. C 
	*/
 
	#include <conio.h>
 
	int main() 
	{ 
	   char buffer[22], *return_value; 

	   buffer[0] = sizeof (buffer) -2; 
	   _cprintf(" Type something: "); 
	   return_value = _cgets (buffer); 
	   _cprintf("\n\rsize = %d\n\rbuffer = 
		     '%s'\n\rreturn value = '%s'\n\r", 
	      buffer[1], buffer + 2, return_value); 
	   return 0;
	}
Output

C:\SC\EXAMPLES>_cgets Type something: This is something
size = 17
buffer = 'This is something'
return value = 'This is something'

_cprintf

Header

conio.h

Prototype

int _cprintf(const char *format, arg0,... argn);

Description

The _cfprint function writes formatted output to the console, using the function _putch to output the characters. The format argument points to a null-terminated string describing how to format the characters. The formatting commands for converting the text to output are described in the fprintf function listing. Optional arguments describe additional formatting information.

Return Value

The number of characters printed.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

fprintf
_putch
_cscanf

Example


	/* 	Example for _cprintf 

		_CPRINTF. C 
	*/ 

	#include <conio.h>
 
	int main() 
	{ 
	  _cprintf(" 1. \\n works different for console 
		      I/ O, it goes down:\n"); 
	  _getch (); 
	  _cprintf(" 2. \\r goes back:\r"); 
	  _getch (); 
	  _cprintf(" 3. \\r\\n goes down and back:\r\n"); 
	  _getch (); 
	  _cprintf(" 4. Like this."); 
	   return 0;
	}
Output

C:\SC\EXAMPLES>_cprintf 1. \n works different for console I/ O, it goes down: 3. \r\n goes down and back: 2. \r goes back: 4. Like this.

_cputs

Header

conio.h

Prototype

int _cputs(const char *string);

Description

The _cputs function writes a null-terminated string to the console.

Return Value

0 if successful; non-zero if unsuccessful.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

_putch

Example


	/* 	Example for _cputs. c 
		SAY. C 
	*/ 

	#include  

	int main(int argc, char *argv[]) 
	{ 
	   int i;
 
	   for (i = 1; i < argc; i += 1) 
	   { 
	      _cputs (argv[i]); 
	      _cputs (" "); 
	   } 
	   return 0;
	}
Output

C:\SC\EXAMPLES> say Hello there, everybody!
Hello there, everybody!

_cscanf

Header

conio.h

Prototype

int _cscanf(char *format, arg0,... argn);

Description

The _cscanf function reads input text directly from the console using the _getche function to read the characters. The format argument points to a null-terminated string that describes how to convert the input. The format for converting the input is described in the fscanf function.

Return Value

The number of fields converted and assigned.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

_cprintf
fscanf
_getche
scanf

Example


	/* 	Example for _cscanf 
		Also demonstrates _cprintf 
	*/
 
	#include <conio.h>

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

	   _cprintf("Enter a number, then a string\n\r"); 
	   _cscanf("%d %s", &a_number, a_string); 
	   _cprintf("\nThe number was %d, the string 
		      was '%s'\n", a_number, a_string); 
	   return 0;
	}
Output

	Enter a number, then a string
	1492 Ambidexterous
	The number was 1492, the string was
	'Ambidexterous'

Copyright © 1995-2002 Digital Mars. All Rights Reserved.