Getting Started
Hello Graphics Program
This page demonstrates a simple graphics.h program that initializes graphics mode, draws a shape, and waits for user input before closing. It is the graphical equivalent of a “Hello, World” program.
On This Page
Complete Example Program
Run This Program (DOS Output)
DOS Output
Step-by-Step Explanation
-
1. Include Headers
#include <graphics.h>provides graphics functions and#include <conio.h>gives keyboard utilities likegetch(). -
2. Declare Driver and Mode
int gd = DETECT, gm;sets up graphics driver and mode variables.DETECTenables auto-detection. -
3. Initialize Graphics
initgraph(&gd, &gm, "");switches to graphics mode, loads the driver, and prepares the screen for drawing. -
4. Draw Shape
circle(200, 200, 100);draws a circle centered at(200, 200)with radius100. -
5. Wait for Input
getch();keeps output visible until the user presses a key. -
6. Close Graphics
closegraph();returns to text mode and releases graphics resources.
Coordinate system reminder: origin is top-left; X increases right, Y increases downward.
How to Run This Program
You can run this example using:
- Online compiler on this website
- VS Code extension
- Turbo C with DOSBox
- Configured MinGW environment (if supported)
Before drawing, make sure graphics mode initializes successfully.
Related setup guide: Where to Run graphics.h.
What You Should See
- The screen switches to graphics mode.
- A circle appears near the center.
- The program waits for a key press.
- After key press, the program exits.
Common Beginner Mistakes
- Forgetting to call
initgraph(). - Forgetting to call
closegraph(). - Not including
graphics.h. - Incorrect BGI path in older Turbo C setups.
- Using graphics.h in compilers that do not support it.
Next recommended topic: line() drawing function.
FAQ
Why is this called the graphics “Hello, World”?
It is the smallest meaningful graphics.h program: initialize graphics, draw one shape, wait, then close.
Can I run this with normal GCC without extra setup?
Not directly. graphics.h is not part of standard C toolchains and needs compatible environments.