BCA TU, C Programming – Unit 12: Data File Handling

Graphics in C allows drawing shapes, displaying colors, and creating visual output using graphical functions.


1. Initialization

  • Graphics require initializing the graphics driver and mode.

#include <graphics.h>

int gd = DETECT, gm;

initgraph(&gd, &gm, “”);  // Initialize graphics mode

  • DETECT → Automatically detects the graphics driver.
  • ” ” → Path to BGI driver (usually empty if BGI files are configured).

2. Graphical Mode

  • Switches program from text mode to graphics mode.
  • After initialization, all graphical functions can be used.
  • Close graphics mode:

closegraph();  // Return to text mode


3. Graphical Functions

  1. Drawing Functions:
  • line(x1, y1, x2, y2) → Draw line between two points
  • rectangle(x1, y1, x2, y2) → Draw rectangle
  • circle(x, y, radius) → Draw circle
  • ellipse(x, y, start_angle, end_angle, x_radius, y_radius) → Draw ellipse
  1. Text and Color Functions:
  • outtextxy(x, y, “Text”) → Display text at position
  • setcolor(color) → Set color for drawing
  • setfillstyle(pattern, color) → Set fill pattern and color
  1. Other Functions:
  • bar(x1, y1, x2, y2) → Draw filled rectangle
  • floodfill(x, y, border_color) → Fill area with color

Key Takeaways

  • Graphics in C requires initializing graphics driver and mode.
  • Use initgraph() to start, closegraph() to exit graphics mode.
  • Common functions: line, rectangle, circle, ellipse, outtextxy.
  • Colors and fill patterns can be set for shapes.
  • Graphics provides visual representation for better user interaction and program output.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.