Parameters
| Name | Type | Description |
|---|---|---|
left | int | The x-coordinate of the top-left corner of the bar. Higher values shift the left edge toward the right. |
top | int | The y-coordinate of the top-left corner of the bar. Higher values shift the top edge downward. |
right | int | The x-coordinate of the bottom-right corner of the bar. Must be greater than left for normal visibility. |
bottom | int | The y-coordinate of the bottom-right corner of the bar. Must be greater than top for normal visibility. |
All coordinate parameters are specified as integer pixel distances from the viewport origin, which defaults to the top-left corner (0, 0) of the screen. For standard VGA mode (which provides a 640 by 480 grid), the horizontal bounds range from 0 to 639 and vertical bounds from 0 to 479. The values passed must satisfy left < right and top < bottom. If these conditions are violated, BGI behavior can vary; in some versions of BGI, the coordinates are swapped to draw correctly, while in others, the function quietly exits or fails to draw anything. It is best practice to always ensure your logic calculates coordinates in the correct spatial order.
Return Value
bar() returns void. It executes the drawing command in the graphics frame buffer and returns control immediately. If the coordinates are fully outside the active viewport, BGI's clipping engine handles it transparently, preventing memory corruption. There are no status indicators returned, so verifying coordinates before drawing is the developer's responsibility.
Code Examples
Basic solid bar
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// Set fill color to Green and style to Solid
setfillstyle(SOLID_FILL, GREEN);
// Draw a green filled bar
bar(150, 100, 450, 380);
getch();
closegraph();
return 0;
}
This program initializes BGI graphics, applies a solid green fill pattern using setfillstyle(), and draws a green rectangle. Since bar() has no outline, the edges are defined cleanly by the green filled pixels against the black background.
Bar chart demonstration
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
int data[] = {150, 280, 200, 350};
int i, x = 100;
initgraph(&gd, &gm, "");
// Draw a baseline
line(80, 400, 500, 400);
for(i = 0; i < 4; i++) {
// Change color for each bar
setfillstyle(SOLID_FILL, RED + i);
// Draw vertical bar extending upwards from baseline (400)
bar(x, 400 - data[i], x + 50, 399);
x += 80;
}
getch();
closegraph();
return 0;
}
This code loops over an array of heights to render a series of colored bars. The top edge is calculated by subtracting the data value from the baseline y-coordinate (400), creating a standard vertical bar chart.
Bordered bar
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// 1. Fill style and draw the interior
setfillstyle(HATCH_FILL, CYAN);
bar(100, 100, 300, 250);
// 2. Set outline color and draw the boundary frame
setcolor(WHITE);
rectangle(100, 100, 300, 250);
getch();
closegraph();
return 0;
}
Since bar() does not draw a border outline, this example combines bar() with rectangle() using the same coordinates. This produces a cyan hatch-patterned bar outlined in white.
Common Mistakes
Confusing bar() with rectangle()
A common pitfall is expecting bar() to draw a border. If you want a box with a different border color and fill color, you must call both bar() (for the fill) and rectangle() (for the border), or use bar3d() with a depth of 0.
Incorrect coordinate order
Passing a left value greater than right or a top value greater than bottom is a common mathematical error in dynamic coordinate calculations. Ensure that (left, top) is always the top-left coordinate, and (right, bottom) is the bottom-right coordinate.
Forgetting setfillstyle()
If you call bar() without setting the fill style first, it will default to the standard fill (usually solid white). Always set the fill style before drawing your filled shapes to ensure visual correctness.
Drawing sequence issues
Because BGI has no layers, shapes are drawn directly onto the screen buffer in sequential order. If you draw text with outtextxy() and then draw a bar() over the same coordinates, the bar will overwrite and hide the text. Always render backgrounds and bars first, then render outlines and text on top.
Try it online
Paste any of the example codes into our browser-based compiler panel to run C graphics code immediately without installing Turbo C++ or configuring paths.