Graphics Function in C Programming (Types, Syntax & Examples) | C Graphics Tutorial 2025
Learn Graphics Function in C programming, including types of graphics functions, BGI library usage, and practical examples for drawing shapes and building 2D graphics in C.
Thank you for reading this post, don't forget to subscribe!What is a Graphics Function in C?
A Graphics Function in C is a predefined function provided by the BGI (Borland Graphics Interface) library that allows programmers to draw, manipulate, and display graphics.
- These functions are essential for creating 2D graphics, interactive programs, simulations, and games in C.
Common graphics functions include drawing lines, circles, rectangles, polygons, and text on the screen.
Basic Syntax of Graphics Functions
Before using graphics functions, initialize graphics mode:
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Graphics functions here
closegraph(); // Close graphics mode
return 0;
}initgraph(): Initializes graphics systemclosegraph(): Frees memory and closes graphics modesetcolor(color): Sets the color for shapes or text
⚡ Common Graphics Functions in C
| Function | Description | Example |
|---|---|---|
line(x1,y1,x2,y2) | Draws a line between two points | line(50,50,200,200); |
rectangle(x1,y1,x2,y2) | Draws a rectangle | rectangle(100,100,200,200); |
circle(x,y,r) | Draws a circle with radius r | circle(300,200,50); |
ellipse(x,y,xstart,xend,rx,ry) | Draws an ellipse | ellipse(200,200,0,360,50,80); |
outtextxy(x,y,"text") | Displays text at specified coordinates | outtextxy(150,250,"Hello C Graphics"); |
setcolor(color) | Sets drawing color | setcolor(RED); |
fillellipse(x,y,rx,ry) | Draws a filled ellipse | fillellipse(300,300,50,30); |
bar(x1,y1,x2,y2) | Draws a filled rectangle | bar(50,50,150,150); |
Example: Using Multiple Graphics Functions
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(RED);
rectangle(100, 100, 300, 200);
setcolor(BLUE);
circle(400, 200, 50);
setcolor(GREEN);
line(50, 50, 400, 400);
outtextxy(150, 250, "Graphics Functions Example");
getch(); // Wait for key press
closegraph(); // Close graphics mode
return 0;
}Output: Draws a red rectangle, blue circle, green line, and displays text.
Applications of Graphics Functions in C
- Game Development: Create 2D games with shapes, movements, and collisions.
- Educational Tools: Visualize concepts in math, physics, or programming.
- Data Visualization: Draw charts, graphs, and plots.
- Simulations: Build interactive applications for engineering or science.
- GUI Design: Basic graphics for buttons, menus, and interactive elements.
Best Practices for Graphics Programming
- Always initialize graphics mode before calling graphics functions.
- Use consistent coordinate systems for drawing multiple shapes.
- Combine
setcolor()with shapes to enhance visuals. - Call
closegraph()at the end to free memory. - Avoid excessive use of function calls in loops to prevent flickering; use double buffering if necessary.
Advantages of Graphics Functions
- Makes programs interactive and visually appealing.
- Enhances learning and understanding through visualization.
- Provides low-level control for graphics in C.
- Useful for educational, gaming, and simulation applications.
Conclusion
Graphics Functions in C are fundamental for building interactive, dynamic, and visually rich programs.
By mastering BGI graphics functions like line(), circle(), rectangle(), and outtextxy(), developers can create 2D games, simulations, and educational tools efficiently.