Shape drawing function

bar() in graphics.h

void far bar(int left, int top, int right, int bottom);

The bar() function draws a filled rectangle (a "bar") on the active graphics screen. Unlike rectangle(), which only renders the outline frame of a box using the current drawing color, bar() fills the entire rectangular region. It utilizes the pattern and color configured via the setfillstyle() function. Crucially, the bar() function does not draw any border outline around the box. The boundary of the bar is defined solely by the limits of the fill pattern itself.

In classical Borland Graphics Interface (BGI) program design, bar() is a primary tool for constructing user interface panels, custom buttons, bar charts, loading indicators, and solid background boxes. Since BGI does not feature built-in GUI components, developers rely on combinations of bar() and outtextxy() to create menu bars and buttons. When designing charts, multiple vertical or horizontal bars are plotted sequentially to visually represent data vectors. Because it draws without an outline, it is also frequently used to clear specific rectangular segments of the screen by drawing a bar filled with the background color.

Parameters

NameTypeDescription
leftintThe x-coordinate of the top-left corner of the bar. Higher values shift the left edge toward the right.
topintThe y-coordinate of the top-left corner of the bar. Higher values shift the top edge downward.
rightintThe x-coordinate of the bottom-right corner of the bar. Must be greater than left for normal visibility.
bottomintThe 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.