Screen control function

cleardevice() in graphics.h

void far cleardevice(void);

The cleardevice() function clears the entire active graphics screen and repaints it using the current background color. In classic BGI programs, it is one of the simplest ways to remove all existing drawings before starting a fresh frame, a new scene, or a different menu state.

This function is especially useful in redraw loops. Many beginners first use it during moving-object demos because the screen needs to be wiped before the object is drawn at its next position. It is different from clearviewport(), which only clears the current viewport region. If you want to reset the full graphics area, cleardevice() is the broader tool.

Parameters

NameTypeDescription
nonevoidcleardevice() does not accept any arguments. It clears the entire active graphics device.

Because there are no parameters, the main thing to understand is scope. The function clears all visible graphics content in the active drawing area. If your background color is black, the screen becomes black. If you changed it with setbkcolor(), the cleared screen uses that new background color instead.

Return Value

cleardevice() returns void. It does not report how many pixels were cleared or whether anything was previously drawn. Its role is purely state-changing: remove the current screen contents and give the program a clean surface to draw on again.

Code Examples

Clear the screen between two drawings

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

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

    setcolor(YELLOW);
    circle(200, 180, 70);
    getch();

    cleardevice();
    setcolor(LIGHTCYAN);
    rectangle(160, 120, 420, 320);

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

The user first sees a circle. After a key press, the full screen is cleared, and a rectangle is drawn instead. This makes it easy to stage drawings one after another.

Simple animation loop

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

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

    for (x = 60; x <= 540; x += 8) {
        cleardevice();
        setcolor(LIGHTGREEN);
        circle(x, 220, 25);
        delay(35);
    }

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

This loop clears the previous frame before drawing the ball again. Without the clear step, every old position would remain visible and create a trail.

Reset before drawing a labeled chart

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

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

    cleardevice();
    setcolor(WHITE);
    rectangle(80, 80, 560, 360);
    outtextxy(220, 50, "Fresh chart canvas");

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

This example shows how some programs call cleardevice() immediately before building the next screen state, especially after returning from another menu or drawing mode.

Common Mistakes

Expecting it to clear only one area

cleardevice() clears the full graphics device. If you only want to wipe a viewport subregion, use clearviewport() instead.

Forgetting the background color affects the result

The cleared screen uses the active background color. If you changed that color earlier, the screen may not return to black unless you set it back explicitly.

Using it in every loop without considering redraw cost

For small classroom demos this is fine, but constant full-screen clears can cause visible flicker in simple animation programs. Sometimes a smaller redraw area is enough.

Try it online

Run the examples in the browser compiler and compare what happens when you remove the clear step from the animation loop.