Error Codes
Error Codes in graphics.h
Use graphics error functions to check if initialization succeeded and to print readable error messages. Each function is explained briefly with a simple snippet.
On This Page
graphresult()
Returns the last graphics error code.
int err = graphresult();
if (err != grOk) {
// handle error
}
grapherrormsg()
Converts an error code into readable text.
char *msg = grapherrormsg(err); outtextxy(20, 20, msg);
Common Error Constants
grOk: No error.grNoInitGraph: Graphics not initialized.grNotDetected: Graphics hardware not detected.grFileNotFound: Driver file not found (legacy setups).grInvalidDriver: Invalid graphics driver.
Full Program
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
int err;
char msg[120];
initgraph(&gd, &gm, "");
err = graphresult();
if (err != grOk) {
sprintf(msg, "Graphics error: %s", grapherrormsg(err));
outtextxy(20, 20, msg);
getch();
return 1;
}
setcolor(WHITE);
outtextxy(20, 20, "Graphics initialized successfully.");
outtextxy(20, 45, "graphresult() == grOk");
rectangle(80, 90, 260, 220);
getch();
closegraph();
return 0;
}