How to Use cleardevice() function in C Programming Language

cleardevice() function in C Programming Language

Introduction :

Using C or C++ programming language, creating a visually appealing program is a fascinating and exciting feature of software development. One essential function used in C or C++ graphics programming language is cleardevice() function.

One important part of graphics programming is managing the display screen. Sometime you want to clear the graphics screen to prepare for fresh drawing on the screen or graphics window. For achieving this task, cleardevice() function of graphics.h library of Turbo C++ IDE plays a vital role. This function allows you to refresh your graphical canvas or screen.

In this article, I shall explore you how to use the cleardevice() function of the graphics.h library in the Turbo C++ IDE using C or C++ programming language.

What is cleardevice() function :

The cleardevice() function is not a standard function in the C or C++ programming language. It is a function of the graphics.h library of Turbo C++ IDE. In the graphics mode, you can start drawing graphics on the screen. At that time, you want to clear the screen to start fresh drawing to create an animation. You can use the cleardevice() function to clear the graphics screen. It allows you to draw new graphics or create animations by repeatedly clearing and redrawing the screen.

This function erases all previously drawn graphics, text or shapes of the graphics screen and reset it to its initial state with its background and drawing color set using setbkcolor() and setcolor() functions respectively. It is very useful when you want to refresh the graphics screen or create an animation program which continuously update the graphics screen with fresh drawing.

It is commonly used in various programs like animation program, gaming program and more.

syntax of cleardevice() function :

The following code is the syntax of cleardevice() function.

void cleardevice();

Here you see that cleardevice() function does not take any argument. It does not return any value.

When you call cleardevice() function in your program, it clears the current graphics screen and remove all existing graphics or drawings of graphics window or canvas. It also allows you to start with a fresh and empty graphics screen for new drawings.

About the program :

In the program, you see a rectangle on the graphics screen. After some time, the screen is cleared and another rectangle is displayed. The background and drawing color will not change. When you press any key, the program will stop.

Explanation of the program :

At first, include graphics.h library for graphics functions, conio.h library for getch() function and dos.h library for delay() function in the program. In the main() function, declare two integer variables “graphic_driver” and “graphic_mode” for graphics driver and mode. Using the initgraph() function, initialize the graphics system and create a graphics window.

The setbkcolor() and setcolor() functions set the background and drawing color of the graphics window respectively. Then, draws two rectangles one by one using rectangle() function. Between two rectangles, use delay() function that allows you to see the first rectangle on the screen.

Now using cleardevice() function, clear the graphics window and remove the previously drawn rectangle . After that, take a key press from user using getch() function that allows you to see the second rectangle. Finally, close the graphics window using closegraph() function.

How run the program :

To run the above program, first install the Turbo C++ IDE on your pc. Then, open the Turbo C++ IDE to create a C or C++ file with .c or .cpp extension. After that, copy the below code of the program and paste into your C or C++ file in the Turbo C++ IDE. To know how to copy paste in the Turbo C++ IDE follow my link. You can also see my other link to know how to use graphics.h in Turbo C++ IDE.

Finally, you can compile and run the program on your pc to see how to use cleardevice() function using C or C++ programming language.

Code of the program using cleardevice() function :

Below code is used for cleardevice() function using C or C++ graphics programming language.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main()
{
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(10);
    setcolor(4);
    rectangle(100, 100, 300, 300);
    delay(1000);
    cleardevice();
    rectangle(300, 100, 500, 300);
    getch();
    closegraph();
    return 0;
}

Output :

When you run the program, you see the image of the output on your pc.

output of cleardevice() function by c language

Conclusion :

After reading the above article, you have learned the purpose and basic usage of cleardevice() function of graphics.h library of the Turbo C++ IDE using C or C++ programming language. Using the cleardevice() function, you can create different type of graphics program to bring your creative visions to life on the digital canvas. Thank you for visiting my site.

Scroll to Top