Bar() in c – C Program to Draw a Rectangle and Bar Using C Graphics

Write a program in C to draw a rectangle and a bar on screen using graphics.h header file

Bar() in c: In this program, we will draw a rectangle and a bar on screen. We will use rectangle and bar functions of graphics.h header file to draw rectangle and bar on screen. Below is the detailed descriptions if these two functions.

void rectangle(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);

rectangle function draws a rectangle on screen. It takes the coordinates of top left and bottom right corners.

void bar(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);

bar function draws a rectangle and fill it with current fill pattern and color.

Function Argument Description
xTopLeft X coordinate of top left corner.
yTopLeft Y coordinate of top left corner.
xBottomRight X coordinate of bottom right corner.
yBottomRight Y coordinate of bottom right corner.

C program to draw rectangle and bar using graphics

C Program to Draw a Rectangle and Bar Using C Graphics

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
 
int main(){
   int gd = DETECT,gm;
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   /* Draw rectangle on screen */
   rectangle(150, 50, 400, 150);
 
   /* Draw Bar on screen */
   bar(150, 200, 400, 350);
 
   getch();
   closegraph();
   return 0;
}

Program Output

RECTANGLE