Fact-checked by Grok 2 weeks ago

conio.h

conio.h is a non-standard header file originally developed by for their , introduced in 1987, targeting environments, providing functions for direct console input and output operations. It enables low-level manipulation of the text-based console, including unbuffered keyboard input, screen clearing, cursor positioning, and text attribute control, which were essential for creating interactive applications in the absence of graphical interfaces. The header's functions bypass standard I/O buffering to allow responses, making it particularly valuable for DOS-era programming tasks such as game development, menu systems, and password entry. Borland's implementation, detailed in their 1992 library reference, includes core routines for reading characters (getch(), getche()), checking key presses (kbhit()), outputting text (cputs(), cprintf()), and managing display elements (clrscr(), gotoxy(), textcolor()). Microsoft provides similar functionality in their C runtime library (CRT) for Windows and DOS compatibility, including functions prefixed with underscores (e.g., _getch(), _kbhit(), _putch()) to support unbuffered console I/O in console applications. Although conio.h is not part of the ISO C standard and has been largely superseded by platform-specific APIs like POSIX termios or Windows Console API in modern systems, it persists in some compilers (e.g., DJGPP, with partial support in MinGW) for legacy and educational purposes.

Overview

Purpose and Scope

conio.h is a non-standard header file in the C and C++ programming languages, providing a collection of functions for console input and output operations primarily targeted at text-mode environments. It was first developed for MS-DOS compilers, notably popularized by Borland compilers, offering developers tools to interact directly with the console hardware. The core scope of conio.h encompasses low-level access to console features, such as unbuffered input, screen clearing, cursor positioning, and attribute for text , which enable real-time manipulation of text-based user interfaces. These capabilities are not part of the standard library's stdio.h, which focuses on buffered, file-like stream I/O for portability across platforms. In contrast, conio.h emphasizes direct, non-portable over console hardware, supporting operations like reading keys without echo or altering screen colors and positions immediately. Key use cases for conio.h include DOS-era applications, game development where immediate response is essential, and simple text-based user interfaces requiring direct screen manipulation, such as clearing the display or moving the cursor without overhead. For instance, it facilitates unbuffered input via functions like getch() and screen resets with clrscr(), supporting efficient text-mode programming in legacy environments.

Historical Development

The header was originally developed by Lattice Inc. for their Lattice C compiler, released in 1982 as the first C compiler for the PC, where its functions mapped to early interrupts. conio.h was popularized in 1987 by International as part of their compiler suite for , specifically designed to facilitate efficient console input and output programming on PC-compatible systems. The header provided low-level access to video memory and interrupts, enabling developers to perform direct screen manipulations without relying on the slower , which simplified the creation of interactive text-based applications. In the 1990s, the library evolved alongside Borland's progression to Borland C++ and Turbo C++, with enhancements that supported advanced console features such as color attributes, cursor positioning, and interrupt-based keyboard handling, broadening its applicability for more sophisticated text-mode interfaces. included conio.h functionality in their C starting with version 4.0 in 1986, which was later carried over to Visual C++ compilers in the 1990s. Additionally, open-source efforts like , a of Collection to released in the early 1990s, revived and extended conio.h functionality for 32-bit environments, maintaining its relevance for legacy DOS development. The header reached its zenith of usage during the era of the 1980s and 1990s, powering a wide array of , utilities, and rudimentary games that leveraged its straightforward for console interactions. However, post-2000, conio.h experienced a marked decline as the landscape shifted toward graphical user interfaces in Windows and adherence to standards in systems; its platform-specific nature rendered it incompatible with ISO C, which does not include or endorse such extensions.

Functions

Input Functions

The input functions in conio.h provide mechanisms for reading input in a non-buffered, direct manner, which is particularly useful for interactive console applications requiring immediate response without waiting for presses. These functions originated in environments and are designed to bypass standard input buffering, allowing developers to capture keystrokes as they occur. The getch() function reads a single from the console without echoing it to the screen or requiring a , returning an value representing the . For ordinary keys, it returns the ASCII code; however, for extended keys such as or keys, the first call to getch() returns 0 (or 0xE0 for certain keys), and a subsequent call returns the specific scan code of the key. This two-byte sequence enables detection of non-printable keys in applications like text editors or games. In contrast, getche() operates similarly to getch() but echoes the read character to the console immediately upon input. It also handles extended keys via the same two-call mechanism, returning 0 or 0xE0 first, followed by the scan code. This echoing behavior makes getche() suitable for scenarios where visual feedback is desired without full line buffering, such as password entry with visible characters or simple prompts. The kbhit() function checks whether a key has been pressed without blocking or consuming the input, returning a non-zero value if a keystroke is available in the input buffer and zero otherwise. It is commonly paired with getch() or getche() in polling loops to enable non-blocking input handling, preventing programs from halting execution while waiting for user interaction. For instance, in game development, a loop might use if (kbhit()) { int ch = getch(); /* process key */ } to respond to player inputs during rendering cycles without interrupting the main program flow. Handling special keys involves checking for the initial 0 return from getch() and reading the follow-up scan code to identify actions like movement (e.g., returning codes 72h for up, 80h for down). Additional input functions include ungetch(int ch), which pushes the character ch back into the input buffer so that a subsequent getch() or getche() will return it, with prototype int ungetch(int ch); and returning the pushed character on success. The getpass(const char *prompt) function prompts for a password without echoing input, returning a pointer to a static string containing the entered password (limited to 8 characters), prototyped as char *getpass(const char *prompt);. For buffered string input, cgets(char *str) reads a line into str, stopping at a specified width and handling the terminating , with prototype char *cgets(char *str); and returning a pointer to the string on success. Internally, these functions in the original MS-DOS implementation rely on BIOS interrupt 16h (INT 16h) for low-level keyboard services, where subfunction AH=00h waits for and retrieves a keystroke from the type-ahead buffer, returning the ASCII code in AL and scan code in AH. Subfunction AH=01h is used by kbhit() equivalents to test for key availability without removal, and AH=10h supports enhanced keyboards for extended key handling. This BIOS-level access ensures direct hardware interaction, though modern implementations in Windows translate these to console API calls for compatibility.

Output and Screen Functions

The output and screen functions in conio.h provide low-level mechanisms for rendering text directly to the console display, enabling efficient screen manipulation without relying on buffered I/O or formatting routines like . These functions are particularly suited for text-mode environments on PC compatibles, where they facilitate updates for applications such as games or interactive tools. When the directvideo is set to 1, many of these operations perform direct writes to video memory for speed, bypassing interrupts. The putch function outputs a character to the console at the current cursor position within the defined text window. Its prototype is int putch(int c);, where c is the ASCII to . It returns the character written upon success or EOF (-1) if an error occurs, such as an invalid output position. Unlike standard output functions, putch does not interpret characters (\n) as return-linefeed pairs and advances the cursor accordingly after writing. In direct video mode, it writes the character and its associated attribute byte directly to screen memory. The cputs(const char *str) function outputs a to the console without a trailing , advancing the cursor after the last , with prototype int cputs(const char *str); and returning the last printed or EOF on error. Similarly, cprintf(const char *format, ...) performs formatted output directly to the console, similar to but bypassing standard I/O buffering, with prototype int cprintf(const char *format, ...); and returning the number of characters output. The clrscr function clears the entire screen by filling it with spaces using the current text background color and repositions the cursor to the home position (1,1). Its prototype is void clrscr(void);, with no return value. This operation affects the full screen or the current text window if one is defined, resetting video memory to a blank state for fresh output. Cursor positioning is restored to the upper-left corner after clearing, ensuring alignment for subsequent writes. The clreol function erases the portion of the current line from the cursor position to the end of the line, filling it with spaces based on the current background color, while leaving the cursor in place. Its prototype is void clreol(void);, and it returns nothing. This allows partial line refreshes without disrupting the entire screen or moving the cursor, making it useful for overwriting trailing text. In , it modifies video memory only for the affected segment of the line within the current window boundaries. The delline and insline functions manage line-level screen scrolling by deleting or inserting lines at the current cursor row. The delline function, with prototype void delline(void);, removes the line containing the cursor and shifts all subsequent lines upward, causing the bottom line to scroll off-screen; it has no return value and operates within the current text window on IBM PC-compatible hardware. Conversely, insline, prototyped as void insline(void);, inserts a blank line (filled with spaces using the current background color) at the cursor row, pushing existing lines below it downward and scrolling the bottom line off-screen. Both functions alter video memory by shifting content vertically, enabling smooth scrolling effects without full screen redraws. The defines a rectangular of the screen as the active text , restricting subsequent output operations to that area for localized management. Its is void window(int left, int top, [int](/page/INT) right, [int](/page/INT) bottom);, where coordinates are 1-based absolute screen positions (e.g., left=1, top=1, right=, bottom=25 for a full 80x25 screen in standard ); it returns nothing. The minimum window size is 1x1, and output beyond the bounds is clipped, with the cursor wrapping within the window rather than the full screen. By default, the window encompasses the entire , and changes affect video memory access for all compatible output functions. In color text mode, these operations target segment 0xB800 for direct memory writes when enabled.

Attribute and Cursor Functions

The attribute and cursor functions in conio.h provide mechanisms for modifying the visual appearance of text output and controlling the position of the text cursor on the console screen, primarily in text-mode environments such as . These functions enable developers to set foreground and background colors, adjust text intensity, retrieve current display information, and manipulate cursor coordinates, facilitating the creation of formatted console interfaces without direct hardware access. Color control is achieved through functions that set the foreground and background attributes for subsequent text output. The textcolor(int color) function sets the foreground color of text, accepting an value or symbolic constant from 0 to 15, where the change applies to all text printed afterward until modified. Similarly, textbackground(int color) establishes the background color for text output, using the same range of values, and affects the fill color behind characters. These functions operate on the standard 16-color palette defined in conio.h (often shared with graphics.h), allowing for combinations that produce up to 256 possible attribute values when foreground and background are paired. The textattr(int newattr) function sets both foreground and background attributes in a single call, using a combined where the low 4 bits are foreground and high 4 bits background, prototyped as void textattr(int newattr);. The color constants are enumerated as follows, representing the VGA-compatible palette used in text modes:
ConstantValueDescription
BLACK0Black
BLUE1Blue
GREEN2Green
CYAN3Cyan
RED4Red
MAGENTA5Magenta
BROWN6Brown
LIGHTGRAY7Light Gray
DARKGRAY8Dark Gray
LIGHTBLUE9Light Blue
LIGHTGREEN10Light Green
LIGHTCYAN11Light Cyan
LIGHTRED12Light Red
LIGHTMAGENTA13Light Magenta
YELLOW14Yellow
WHITE15White
Text intensity can be toggled using highvideo() and lowvideo(), which adjust the foreground color's by setting or clearing the high-intensity bit (adding or subtracting 8 from the color value, respectively). For instance, calling highvideo() on a base color like GREEN (2) effectively sets it to LIGHTGREEN (10), enhancing visibility in console applications. These functions do not alter the background and are useful for emphasizing sections of output without changing the base color palette. The normvideo() function restores normal video attributes, resetting to default colors and , with prototype void normvideo(void);. Cursor positioning functions allow precise control over where text appears on the screen, using 1-based indexing for columns (x) and rows (y). The gotoxy(int x, int y) function relocates the cursor to the specified coordinates within the current text window, with x typically ranging from 1 to 80 and y from 1 to 25 (or up to 43 or 50 in extended modes). Complementary query functions, wherex() and wherey(), return the current column and row positions as integers, enabling programs to track and restore cursor locations dynamically. The textmode(int newmode) function sets the console to a specific , such as C80 for color 80-column or BW40 for black-and-white 40-column, with prototype void textmode(int newmode); and predefined constants in conio.h. To retrieve comprehensive display state, gettextinfo(struct text_info *info) populates a text_info structure with details including the current screen mode, cursor position (via curx and cury fields), attribute settings, and window boundaries (e.g., screenheight, screenwidth). This function is essential for adaptive console programs that need to query the environment before applying attributes or moving the cursor, ensuring compatibility across different s. All these functions assume a text-based console and may behave differently or require in modern non-DOS systems.

Platform and Compatibility

Original and Legacy Support

conio.h originated as a library for and compatible operating systems, where its functions interface directly with services, including for video display operations and related hardware control. This design enabled low-level console manipulation suited to the text-based interfaces of early personal computers, relying on the underlying environment for keyboard and screen handling. Full implementation of conio.h was provided in Borland's and compilers, spanning versions 1.0 (released in 1987) to 4.5 (through the 1990s), which integrated it seamlessly for development on PC compatibles requiring at least 384 KB RAM and an 80-column display. Microsoft Visual C++ 6.0, available prior to 2000, offered partial support for conio.h in console-mode applications, including key functions like _getch for non-echoing input, though many routines were Microsoft-specific extensions rather than complete emulation. Under Windows, conio.h persists in legacy form for Win32 console applications via extensions in _conio.h, but its capabilities are confined to text-mode terminals and do not extend to graphical or environments. The library presumes specific hardware configurations, such as the standard 80x25 for screen layout, and includes provisions for direct hardware port interactions, exemplified by access to port 0x3D4 for cursor control in compatible video adapters. conio.h has never been part of standard distributions, as it deviates from ISO C standards and lacks cross-platform portability, prompting developers to seek alternatives for non- environments. For preservation of legacy code, conio.h functionality is emulated within , a emulator that recreates the runtime to execute programs and similar applications on contemporary hardware.

Modern Implementations and Limitations

In contemporary development environments, conio.h retains limited availability primarily on Windows platforms through compilers such as and , where it provides console I/O functions for legacy compatibility. However, it is explicitly flagged as non-standard and is not recommended for new code due to its platform-specific nature. On systems such as , conio.h lacks native support, as it was designed for MS-DOS-era console hardware not present in modern environments. Emulations are achieved through libraries like PDCurses, a public-domain implementation of the API that wraps terminal capabilities via databases to mimic conio.h behaviors, or custom wrappers around for functions like non-blocking input. These approaches enable portability but require additional dependencies and configuration. Cross-platform development with conio.h encounters significant challenges, as the header is absent from the ISO and relies on non-portable direct console access, often resulting in compilation failures or warnings on strict compilers like or without extensions. This incompatibility stems from its violation of standard C portability rules, which prohibit assumptions about underlying hardware or OS-specific I/O mechanisms. Key limitations of conio.h in modern contexts include its insecurity from legacy designs involving low-level interactions, which can expose vulnerabilities in emulated environments; lack of , as functions like getch() do not synchronize concurrent console access; and poor compatibility with encoding or high-resolution displays, since it assumes single-byte ASCII characters and fixed geometries. These issues make it unsuitable for secure, internationalized, or multi-threaded applications. To address these shortcomings, developers employ workarounds such as open-source polyfills—for instance, the linuxconio library on , which reimplements conio.h functions using APIs for —or direct system calls like Windows' ReadConsole to replicate behaviors such as non-echoing input without requiring the full header. These solutions prioritize standards compliance while approximating the original functionality.

References

  1. [1]
    [PDF] LIBRARY REFERENCE - Bitsavers.org
    ... reference contains an overview of the Borland C++ library routines and header files. The header files are listed; the library routines are grouped according ...
  2. [2]
    _getch, _getwch | Microsoft Learn
    Aug 8, 2025 · The _getch and _getwch functions read a single character from the console without echoing the character. To read a function key or arrow key ...Syntax · Return value
  3. [3]
    _kbhit | Microsoft Learn
    Aug 8, 2025 · The _kbhit function checks the console for a recent keystroke. If the function returns a nonzero value, a keystroke is waiting in the buffer.
  4. [4]
    _putch, _putwch | Microsoft Learn
    Dec 2, 2022 · These functions write the character c directly, without buffering, to the console. In Windows NT, _putwch writes Unicode characters using the current console ...
  5. [5]
    conio.h in C | Scaler Topics
    Jun 27, 2024 · conio.h is a header file in which there are many built-in functions embedded in it they generally perform input/output on the console i.e., it ...
  6. [6]
    [PDF] PROGRAMMER'S GUIDE
    These chapters provide a formal language definition, reference, and syntax for both the C and C++ aspects of. Borland C++. Some overall information for ...
  7. [7]
    Get started programming with DOS conio - Opensource.com
    Sep 2, 2021 · The library includes implementations of conio.h functions that mimic Borland Turbo C++ to set video modes, display colored text, move the cursor ...
  8. [8]
    [PDF] turbo c - Bitsavers.org
    This manual was produced in its entirety with. Sprint: The Professional Word Processor,® available from Borland. Borland International, Inc. 4585 Scotts Volley ...
  9. [9]
    [PDF] • • BORLAND - Bitsavers.org
    Turbo C++ is for C++ and C programmers who want a fast, effi- cient compiler ... library function putch, which is defined in the header file conio.h.
  10. [10]
    Old-school programming with Turbo C - Both.org
    Mar 20, 2025 · ... Borland was an early adopter of ANSI C on DOS. A neat feature in DOS was direct access to video memory using the conio.h family of functions.
  11. [11]
    Compatibility - Microsoft Learn
    Jun 18, 2025 · The Universal C Runtime Library (UCRT) supports most of the C standard library required for C++ conformance. It implements the C99 (ISO/IEC 9899:1999) library, ...<|separator|>
  12. [12]
    _getche, _getwche | Microsoft Learn
    Dec 1, 2022 · The _getche and _getwche functions read a single character from the console with echo, meaning that the character is displayed at the console.
  13. [13]
    RBIL61 - INT 16 - KEYBOARD - GET KEYSTROKE
    INT 16 - KEYBOARD - GET KEYSTROKE AH = 00h Return: AH = BIOS(Basic Input/Output System) A set of standardized calls giving low-level access to the hardware.
  14. [14]
    RBIL61 - INT 16 - KEYBOARD - GET ENHANCED KEYSTROKE ...
    ⇢⇥. INT 16 - KEYBOARD - GET ENHANCED KEYSTROKE (enhanced kbd support only) ... Interrupt List - Release 61 (16jul00) Copyright © 1989-1999,2000 Ralf Brown ...
  15. [15]
    [PDF] BORLAND
    The sample programs included on the Turbo C diskettes provide a demonstration of how to use the various features of Turbo C. They are intended for educational ...
  16. [16]
    Printing To Screen - OSDev Wiki
    The text screen video memory for colour monitors resides at 0xB8000, and for monochrome monitors it is at address 0xB0000 (see Detecting Colour and Monochrome ...Missing: conio. | Show results with:conio.
  17. [17]
    None
    Below is a merged summary of the `conio.h` functions from the Borland C++ Library Reference, consolidating all information from the provided segments into a single, comprehensive response. To maximize detail and clarity, I’ll use a table format for the function summaries, followed by additional notes on color constants and useful URLs. This ensures all details (e.g., prototypes, parameters, descriptions, return values, and color constants) are retained and presented efficiently.
  18. [18]
    Microsoft C/C++ change history 2003 - 2015
    Oct 3, 2025 · The definitions of all of the printf and scanf functions have been moved inline into <stdio.h> , <conio.h> , and other CRT headers. This ...
  19. [19]
    DOSBox, an x86 emulator with DOS
    DOSBox 0.74-3 has been released! A security release for DOSBox 0.74: The game compatibility should be identical to 0.74 and 0.74-2.Downloads · DOSBox Tutorial · Information · ManualMissing: h | Show results with:h
  20. [20]
    Why must we refrain from using conio.h? Is it obsolete?
    Jan 24, 2014 · <conio.h> is non-standard and non-portable. Use a cross-platform library such as ncurses if you need functionality not provided by the standard C libraries.why does this error occur: 'conio.h' file not found - Stack OverflowIs the conio.h header file supported by gcc? - Stack OverflowMore results from stackoverflow.com
  21. [21]
    Do conio.h and stdio.h support in Visual C++ - Developer Community
    Feb 13, 2018 · The conio.h and stdio.h are supported, and we cannot reproduce your issue in our side. Can you find the two header files under location similar ...
  22. [22]
    PDCurses
    ### Summary of PDCurses and conio.h Emulation
  23. [23]
    c - Where is the <conio.h> header file on Linux? Why can't I find ...
    Jan 9, 2012 · conio.h is a C header file used with old MS-DOS compilers to create text user interfaces. Compilers that target other operating systems, such as Linux-based, ...why does this error occur: 'conio.h' file not found - Stack Overflowg++ conio.h: no such file or directory - Stack OverflowMore results from stackoverflow.comMissing: DJGPP source revival 1990s
  24. [24]
    'clrscr()' and 'conio.h' not working - Stack Overflow
    Jul 4, 2015 · In short: Your program is not portable to Unix (or in fact, anything but MSDOS or Win32). So unless you're using an environment that has this ( ...Why the use of "conio.h" is not good habit of programming?Why must we refrain from using conio.h? Is it obsolete?More results from stackoverflow.comMissing: decline non-
  25. [25]
    Why does <conio.h> error saying it doesn't exist in GCC and Clang ...
    Aug 11, 2022 · conio.h is a C header file used mainly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ...I want to use some built-in functions in C++ so the header file conio ...Why do we use #include <conio.h> in C++ programming? - QuoraMore results from www.quora.com
  26. [26]
    Why the use of "conio.h" is not good habit of programming?
    Nov 12, 2013 · conio.h is platform-specific. If you try to compile on Linux, your code will probably not compile. Also - using functions to manipulate the console window make ...Missing: peak DOS era shareware
  27. [27]
    "conio.h" implementation for Linux - GitHub
    This is the implementation of "conio.h" from ms-dos based C compilers for linux/*nix systems. Why use "conio" on linux instead of "curses" or other libraries.