Introduction :
The closegraph() function is an important function that used in C or C++ graphics program. In graphics.h library of Turbo C++ IDE, closegraph() function used to close the graphics mode. It is also used for switching from one graphics mode to another. This function properly release all the resources that were allocated to the graphics driver during the initialization process.
This function is important to use to prevent memory leaks and avoid issues with subsequent graphics output. By the initialization of graphics mode, you can draw shapes, lines and images on the graphics screen by C or C++ programming language.
In this article, I shall explain to you how to use the closegraph() function for proper termination of graphics program in C or C++ programming language. Here, I also show you the syntax and usage of closegraph() function.
What is closegraph() function :
The closegraph() function is a function of the graphics.h library in Turbo C++ IDE. After completing graphics operation, closegraph() function is called to close the graphics mode. It also deallocates all resources (memory) used for graphics initialization. After that, it restores to the default text mode from the graphics mode.
Syntax of the closegraph() function :
In C or C++ programming language the following code is the syntax of the closegraph() function.
void closegraph();
The closegraph() function does not take any argument, and it returns no value.
How run the program :
If you want to run the following programs on your PC, first open Turbo C++ and create a C or C++ file in Turbo C++. First, you have to install the Turbo C++ IDE on your PC. After that, just copy the following code and paste in your C or C++ file. You have to know how to copy paste in the Turbo C++ IDE. If you want to know how to run graphics programs in Turbo C++, click this link.
Different types of usage of closegraph() function :
In this article, I shall discuss with you the following usages of closegraph() function in C or C++ programming language.
Closing graphics window using closegraph() function in C or C++ language :
The primary uses of closegraph() function is to close the graphics window after the graphics output has been generated. It is also used to release all the memory resources that were allocated during the graphics mode initialization and prevents memory leaks. After that, the program control returns to the operating system.
Note that, when the program is finished, you have to close the graphics mode using closegraph() function. Because the graphics mode allocates a large amount of memory and resources. If you do not call closegraph() function at the end, it may cause memory leaks and other issues. After using the graphics mode, call the closegraph() function at the end of the program, is very good practice.
In this program, first include the graphics.h header file. Then initialize the graphics mode using initgraph() function and draw a rectangle using rectangle() function. After that, call closegraph() function to close the graphics window and avoid memory leaks. Before closegraph() function, I use getch() for see the output properly. You can use the below code in your program.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
int main(void)
{
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
setcolor(10);
rectangle(150,100,400,250);
getch();
closegraph();
return 0;
}
Output :
The below image is the output of closing the graphics window using closegraph() function in C or C++ programming language.

Switching between graphics modes using closegraph() function in C or C++ language :
The closegraph() function can be used to switch between graphics modes. In the C or C++ programming language, the initgraph() function is used to initialize the graphics mode for drawing graphics on the screen. The first and second parameter of this function specifies the graphics driver and graphics mode. By changing the graphics driver and graphics mode you can switch between one graphics mode to another.
The following graphics drivers are commonly used in C or C++ programming language.
- DETECT – This driver is used to automatically detect the available graphics card and sets the appropriate driver.
- CGA – This driver is used for drawing graphics on a Color Graphics Adapter.
- EGA – This driver is used for drawing graphics on an Enhanced Graphics Adapter.
- VGA – This driver is used for drawing graphics on a standard VGA monitor.
- SVGA – This driver is used for drawing graphics on a Super VGA monitor.
- HERCULES – This driver is used for the Hercules Graphics Card display.
Here, I show you how you change the graphics mode from EGA to VGA using closegraph() function in C or C++ programming language. First, you have to initialize graphics mode using initgraph() function and set the graphics driver (graphic_driver = EGA;) and mode (graphic_mode = EGAMONOHI;). Then, draw a circle with circle() function. Here, I use setbkcolor() function to set the background color (red) of the graphics screen.
After that, using closegraph() function releases the resources of the current graphics mode. Next, switch to a different graphics mode to set the graphics driver (graphic_driver = VGA;) and mode (graphic_mode= VGAHI;). Then initialize the new graphics mode using initgraph() function. Then, draw a circle with background color blue. Lastly, you have to call closegraph() function for release the resources of the new graphics mode.
/*Developed by Puskar Jasu*/
#include<graphics.h>
#include<conio.h>
int main()
{
int graphic_driver, graphic_mode;
int x,y,r;
graphic_driver = EGA;
graphic_mode = EGAMONOHI;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
x=300;
y=250;
r=100;
setbkcolor(4);
circle(x, y-150, r);
getch();
closegraph();
graphic_driver = VGA;
graphic_mode= VGAHI;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
setbkcolor(1);
circle(x, y, 2*r);
getch();
closegraph();
return 0;
}
Output :
The below image is the output of switching between graphics modes using closegraph() function in C or C++ programming language.

Conclusion :
The closegraph() function is an important function that can be used in various ways depending on the requirements of your program. The primary use of closegraph() function is to close the graphics mode and release the allocated resources to prevent memory leaks. It can also be used for switching between graphics modes.
After follow my article, I think you would understand how to use closegraph() function in C or C++ programming language. Thank you for visiting my site.