Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

Turbo C: conio.h and graphics.h

Many schools still teach C with Turbo C, an old compiler for MS-DOS with two famous extra headers: conio.h for coloured text and key presses, and graphics.h for drawing. They are not part of standard C, but our editor supports them, so those programs run in your browser.

conio.h: colours and keys

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

int main(void) {
    clrscr();                     // clear the screen
    textcolor(YELLOW);
    cprintf("Welcome to Turbo C style programs!");
    textcolor(WHITE);
    gotoxy(5, 3);                 // column 5, row 3
    cprintf("Press any key...");
    getch();                      // wait for one key, without Enter
    return 0;
}
You type
x
FunctionDoes
clrscr()clears the screen
gotoxy(x, y)moves the cursor (columns and rows from 1)
textcolor(c), textbackground(c)sets the colours for cprintf
getch()reads one key without showing it or waiting for Enter
kbhit()tells whether a key is waiting, without stopping

graphics.h: drawing

initgraph opens a graphics window of 640 by 480 pixels with 16 colours; (0, 0) is the top-left corner and y grows downwards. closegraph closes it.

#include <conio.h>
#include <graphics.h>

int main(void) {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");

    setfillstyle(SOLID_FILL, LIGHTBLUE);
    bar(0, 0, 639, 330);                  // sky
    setfillstyle(SOLID_FILL, GREEN);
    bar(0, 331, 639, 479);                // grass
    setcolor(YELLOW);
    circle(540, 80, 40);                  // sun
    setfillstyle(SOLID_FILL, YELLOW);
    floodfill(540, 80, YELLOW);
    setcolor(WHITE);
    rectangle(200, 220, 360, 360);        // house
    line(200, 220, 280, 150);             // roof
    line(280, 150, 360, 220);
    outtextxy(20, 20, "My first drawing in C");

    getch();
    closegraph();
    return 0;
}
You type
x

Common functions: line, circle, rectangle, bar, ellipse, arc, putpixel, floodfill, outtextxy, setcolor and setfillstyle. getmaxx() and getmaxy() return 639 and 479, the last pixel in each direction.

Animation

Clear the screen, draw the next frame, wait a little with delay from dos.h, and repeat. kbhit lets the user stop it:

#include <conio.h>
#include <dos.h>
#include <graphics.h>

int main(void) {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");
    for (int x = 40; x < 600 && !kbhit(); x += 8) {
        cleardevice();
        setfillstyle(SOLID_FILL, LIGHTRED);
        fillellipse(x, 240, 25, 25);
        delay(30);
    }
    closegraph();
    return 0;
}

Old style and modern style

Turbo C programs often write void main(). Our editor runs it but warns: standard C requires int main(void) and return 0. Use the modern form in new programs, and remember that conio.h and graphics.h exist only in some compilers; standard C programs use printf and scanf.

Practice exercises

  1. Draw a traffic light: a dark rectangle with red, yellow and green circles.
  2. Draw a bar chart of five values with bar and label each bar with outtextxy.
  3. Move a ball from left to right and back again until a key is pressed.