Parameters
| Name | Type | Description |
|---|---|---|
none | void | closegraph() does not accept any arguments. It closes the currently active graphics mode. |
Because there are no parameters, the function is simple to call. The important part is placement. Put it after the final drawing and after any pause or animation loop. In programs with early error returns, call it only after graphics mode was successfully initialized.
Return Value
closegraph() returns void. It does not report a cleanup status. If your program failed during startup, check graphresult() after initgraph() instead of relying on cleanup to reveal the problem.
Code Examples
Standard close sequence
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(YELLOW);
circle(320, 240, 100);
getch();
closegraph();
return 0;
}
This program shows the most common lifecycle. The circle remains visible until a key is pressed, then closegraph() restores the previous mode.
Close after a small animation
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
int x;
initgraph(&gd, &gm, "");
for (x = 80; x <= 520; x += 8) {
cleardevice();
setcolor(LIGHTGREEN);
circle(x, 220, 35);
delay(35);
}
getch();
closegraph();
return 0;
}
The output is a moving circle. Cleanup happens only after the loop completes and the user presses a key, so the animation is not interrupted by an early mode reset.
Close only after successful initialization
#include <graphics.h>
#include <stdio.h>
#include <conio.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;
}
outtextxy(150, 150, "Initialized successfully");
getch();
closegraph();
return 0;
}
This structure avoids trying to continue drawing after a failed startup. The cleanup function is used only in the successful graphics path.
Common Mistakes
Closing before the user can see output
Place getch() or another pause before closegraph(). Otherwise the drawing can disappear immediately.
Calling drawing functions after cleanup
Once graphics mode is closed, do not call circle(), line(), or other graphics functions unless you initialize graphics mode again.
Skipping cleanup in examples
Small programs may appear to work without cleanup, but complete examples should close graphics mode clearly. It teaches the correct lifecycle and keeps local environments tidy.
Try it online
Run the examples in the browser compiler and test what happens when you move getch() before or after cleanup.