← Back to C Course | Chapter 13: Graphics | Lesson 3 of 10

C Drawing Shapes

You can draw shapes like circles, rectangles, ellipses and polygons on the screen with simple commands, like using shape stamps. You can also fill them in.
Syntax
c
circle(x, y, radius);
rectangle(left, top, right, bottom);
ellipse(x, y, start_angle, end_angle, x_radius, y_radius);

Drawing Circles

circle() draws a wireframe circle outline given a center coordinate (X, Y) and a radius in pixels, tracing just the boundary rather than filling the interior — for a solid disc you'd reach for fillellipse() with equal horizontal and vertical radii instead.

Example: Drawing Circles

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	circle(150, 150, 50);
	getch();
	closegraph();
	return 0;
}

Drawing Rectangles

rectangle() draws an outlined box from four coordinates: the top-left corner's X and Y, followed by the bottom-right corner's X and Y, so the function itself computes the width and height from the distance between those two corner points rather than taking them directly.

Example: Drawing Rectangles

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	rectangle(50, 50, 200, 150);
	getch();
	closegraph();
	return 0;
}

Drawing Ellipses

ellipse() draws an elliptical arc and takes eight parameters in total: the center coordinates, a start and end angle in degrees (letting you draw a partial arc rather than a full ellipse), and separate horizontal and vertical radii, which is what lets it draw true ellipses rather than only perfect circles.

Example: Drawing Ellipses

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	ellipse(150, 150, 0, 360, 80, 40);
	getch();
	closegraph();
	return 0;
}

Filled Shapes

fillellipse() and bar() draw solid, filled shapes instead of outlines — fillellipse() fills an elliptical region, while bar() fills a rectangular one, and bar3d() extends bar() with an extra depth parameter to render a pseudo-3D block with visible side faces.

Example: Filled Shapes

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	fillellipse(150, 150, 60, 40);
	bar(50, 50, 150, 100);
	getch();
	closegraph();
	return 0;
}

Drawing Polygons

drawpoly() and fillpoly() both draw arbitrary multi-sided shapes from an array of (X, Y) coordinate pairs you supply as vertices, with drawpoly() tracing just the polygon's outline and fillpoly() additionally filling its interior with the current fill style.

Example: Drawing Polygons

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	int points[6] = {100, 50, 150, 150, 50, 150};
	drawpoly(3, points);
	getch();
	closegraph();
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Drawing shapes before calling initgraph, so nothing appears.
  2. Mixing up the order of arguments in rectangle(left, top, right, bottom), which gives a wrong or empty shape.
  3. Expecting circle(x, y, r) to be filled, when it only draws an outline unless you also use floodfill or a fill style.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.