Timing function

delay() in graphics.h

void delay(unsigned int milliseconds);

The delay() function pauses program execution for a specified number of milliseconds. In graphics.h programs, it is commonly used to slow down animation loops so motion is visible to the viewer. Without a pause, a loop that clears and redraws the screen may complete too quickly, making the output look like a flicker or a final still frame.

delay() is a simple teaching tool rather than a modern high-precision timer. It blocks the program while it waits, so no other work runs during the pause. For beginner BGI assignments, that is usually acceptable because the goal is to see basic movement: a bouncing ball, moving car, loading bar, clock hand, or step-by-step drawing algorithm.

Parameters

NameTypeDescription
millisecondsunsigned intThe approximate pause duration in milliseconds. 1000 is about one second, and 50 is a short animation frame delay.

A smaller value makes animation faster, while a larger value makes it slower. Values around 20 to 60 milliseconds are common for simple motion. Values above 500 milliseconds are better for step-by-step teaching displays where each state needs to remain visible before the next one appears.

Return Value

delay() returns void. It does not return elapsed time and does not indicate whether the wait was exact. Timing can vary by runtime environment, emulator, browser, CPU load, and graphics backend. Use it as a practical pause, not as a stopwatch-grade measurement tool.

Code Examples

Pause before showing the final screen

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

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

    setcolor(YELLOW);
    outtextxy(180, 180, "This message waits for two seconds");
    delay(2000);

    outtextxy(180, 220, "Now press any key to exit");
    getch();
    closegraph();
    return 0;
}

The first message stays on screen for about two seconds before the second message appears. This is useful for splash screens, loading notices, or guided demonstrations.

Move a ball across the screen

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

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

    for (x = 60; x <= 560; x += 8) {
        cleardevice();
        setcolor(LIGHTCYAN);
        circle(x, 220, 30);
        delay(35);
    }

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

The loop clears the screen, redraws the ball at a new x-coordinate, and waits briefly. The delay controls the speed of movement.

Step through a line drawing process

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

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

    setcolor(LIGHTGREEN);
    for (x = 100; x <= 500; x += 10) {
        line(100, 260, x, 260);
        delay(80);
    }

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

This program reveals a horizontal line gradually. The slower delay makes each extension visible, which is helpful when explaining incremental algorithms.

Common Mistakes

Using delay() as an input wait

delay() waits for time, not for a key press. Use getch() when you want the user to decide when to continue.

Making animation too slow

A delay of 1000 inside a movement loop creates one frame per second. For smooth beginner animations, start closer to 30 or 50.

Clearing the screen without redrawing enough

Animation loops often use cleardevice(). After clearing, redraw every object that should remain visible before calling delay().

Try it online

Run the animation examples and change the millisecond value to feel how timing affects motion.