Wednesday, May 19, 2010

How to use <conio.h> with GCC

This article will teach you how to use <conio.h> header file while programming in C under linux environment with GCC, and getting unbuffered input from the user.

Most of the students are taught C/C++ with Turbo C/C++ IDE in schools. Turbo C has a header file(unspecified in ANSI standard) <conio.h> with which programmer can program for unbuffered input using functions like getch(). Also there are other functions like gotoxy() etc. which work perfect in DOS environment with Turbo C.
After they learn about linux, and then GCC they start practicing in linux. If you are such an student, read on. This will be handy for you.
At some point of time you will write a program in which you want interactive user input from the user. Interactive in the sense that you want an action to be performed as soon as a key is pressed. But since standard library function getchar() buffers the input until ENTER key is pressed, it will be quite irritating for you. So, you'll miss your <conio.h> header file that you used in Turbo. Don't worry. I faced the same problem and I ran around like everything to find a soluion. And finally, I found a solution!!!

Just download the libconioh from this link
Install it. Here are the steps(I'm considering Ubuntu as the example):
1. Copy the downloaded archive to your home folder
2. Open the terminal, by pressing Alt+F2 and typing xterm and pressing enter.
3. Become root by typing
sudo -s
and supplying your password.
4. Type

tar -zxvf libconio-1.0.0.tar.gz
and press enter to extract the archive. To make the typing easy you may press the <tab> key on your keyboard after typing l of libconio-1.0.0.tar.gz for autocompletion of filename.
5. Now enter into the extracted directory by typing
cd libconio-1.0.0
and pressing enter.
6. Install the library by typing these commands one after another's execution.


make install 
 Now the library has been installed. How to use it for programming?
Just #include <conio.h> in your program. And when compiling your program(eg. myprogram.c in current working directory) using gcc give the library's detail to the linker like this:
gcc myprogram.c -lconio
You will get the a.out file if your program doesn't have any errors.
Execute it and enjoy!
But what did that -lconio did? See, in gcc the source files and object library files can be linked by specifying them back to back. Like:
gcc file1.c file2.c lib1.a lib2.a
In linux the library files are with .a extension and they are stored in /usr/lib/ directory with the name format lib<library_name>.a . So when you installed libconioh it copied a file, named to the /usr/lib directory. Now you can compile and link your program that uses conio.h by this command
gcc myprogram.c /usr/lib/libconio.a
The -l switch has been provided as a shortcut for your convenience. This -l makes conio to be read as /usr/lib/libconio.a hence when yo typed gcc myprogram.c -lconio it was interpreted as gcc myprogram.c /usr/lib/libconio.a . Now thats sweet! Isn't it?
Here is a sample program that reads characters from the keyboard, displays the character just next to it(for example, if u enter b it will display c)  and exits only when you press the small a key.

#include <conio.h>
int main(){
char c;
return 0;


Francisco Domene Moros (Pakito) said...

Your link:
is broking.
Could I get de file in other link?
Thanks a lot

prab97 said...

The link is working. Download the files here

Archit Gupta said...

we can use it.. but is there some alternative which is supported by GCC standard library. I dont like to customise my copy of GCC

Unknown said...

when i shifted to root privilage level then i unable to locate and extract libconio.h file said...

-bash permission denied

what to do

prab97 said...

This article was posted 3 years ago. World has moved much forward after during this time period.

Anyways, you can try this:

chmod +x configure

Anonymous said...
This comment has been removed by a blog administrator.

I'm using windows subsystem for Linux and GCC compiler.I want to use conio.h library for a c program. When I followed the above step 'tar -zxcf .....' <I get this error

tar: You may not specify more than one '-Acdtrux', '--delete' or '--test-label' option
Try 'tar --help' or 'tar --usage' for more information.