Graphics startup function

initgraph() in graphics.h

void far initgraph(int far *graphdriver, int far *graphmode, const char far *pathtodriver);

The initgraph() function initializes BGI graphics mode. Almost every classic graphics.h program begins by declaring a graphics driver variable, a graphics mode variable, and then passing their addresses to initgraph(). After this call succeeds, the program can draw shapes, text, pixels, fills, and animations on the graphics screen.

In beginner programs, the most common pattern is int gd = DETECT, gm; followed by initgraph(&gd, &gm, "");. The DETECT constant asks BGI to choose a suitable driver automatically. The path argument tells the runtime where to find BGI driver files such as EGAVGA.BGI when the environment needs them. Online compilers and bundled setups often accept an empty path because the runtime already knows where its graphics assets live.

Parameters

NameTypeDescription
graphdriverint *Pointer to the graphics driver variable. Use DETECT for automatic detection in typical student programs.
graphmodeint *Pointer to the graphics mode variable. BGI writes the chosen mode here after detection.
pathtodriverconst char *Directory path containing BGI driver files. Use an empty string when your environment is already configured.

The first two arguments are pointers, so the address operator & is required when passing ordinary integer variables. If you pass gd instead of &gd, the program will not call the function correctly. The third argument differs by setup: Turbo C on DOS often needs a path like "C:\\TC\\BGI", while browser-based compilers and packaged runners often use "".

Return Value

initgraph() returns void. To check whether initialization succeeded, call graphresult() immediately after initgraph(). A value of grOk means graphics mode is ready. If the result is not grOk, use grapherrormsg() to display a readable startup error before attempting to draw.

Code Examples

Minimal graphics startup

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

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

    outtextxy(100, 100, "graphics.h is ready");

    getch();
    closegraph();
    return 0;
}

This is the smallest practical startup pattern. It enters graphics mode, writes a confirmation string, waits for a key, and closes graphics mode cleanly.

Check initialization errors

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

int main() {
    int gd = DETECT, gm;
    int errorcode;

    initgraph(&gd, &gm, "");
    errorcode = graphresult();

    if (errorcode != grOk) {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        return 1;
    }

    circle(320, 240, 90);
    getch();
    closegraph();
    return 0;
}

This version is better for real debugging. If the graphics driver cannot be loaded, the program prints a message instead of silently failing or drawing on an uninitialized screen.

Use an explicit BGI driver path

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

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TC\\BGI");

    setcolor(LIGHTGREEN);
    rectangle(120, 100, 520, 300);
    outtextxy(180, 190, "Driver path example");

    getch();
    closegraph();
    return 0;
}

Traditional Turbo C installations may require the BGI directory path. If the driver files are in a different folder, adjust the string to match your setup.

Common Mistakes

Forgetting the address operator

The first two arguments must be addresses. Use &gd and &gm, not just gd and gm.

Drawing before initialization

Functions like circle() and outtextxy() require graphics mode. Always initialize first.

Using the wrong driver path

A bad path is a common local Turbo C problem. If the program fails before drawing, check that the BGI files exist at the path you passed.

Try it online

Use the online compiler to test standard initgraph() startup code without installing a local BGI environment.