Pixel plotting function

putpixel() in graphics.h

void far putpixel(int x, int y, int color);

The putpixel() function draws a single pixel at the given screen coordinate using the specified color. It is the lowest-level drawing operation most students use in BGI graphics. Higher-level functions such as line(), circle(), and ellipse() are built around the same idea: deciding which screen pixels should be colored to form a shape.

Because putpixel() works one pixel at a time, it is ideal for understanding raster graphics algorithms. You can use it to implement DDA lines, Bresenham lines, midpoint circles, star fields, particles, noise patterns, and custom plotting logic. It is slower and more verbose than shape functions for ordinary drawing, but it gives complete control over every coordinate and color choice.

Parameters

NameTypeDescription
xintThe horizontal pixel coordinate. In standard VGA-like modes, 0 is the left edge.
yintThe vertical pixel coordinate. In graphics.h, 0 is the top edge and larger values move downward.
colorintThe BGI color index or color constant used for that one pixel, such as WHITE, RED, or YELLOW.

The color argument is direct. It does not depend on the active setcolor() value. That makes putpixel() convenient for multicolor plotting loops where each point may use a different color. Coordinates outside the visible screen are ignored or clipped by the driver, so algorithms should still guard their ranges when they generate many points.

Return Value

putpixel() returns void. It does not report whether the pixel was visible or clipped. If you need to read a pixel after plotting, use getpixel(x, y). If you are drawing many points, validate graphics initialization with graphresult() after initgraph() before entering the plotting loop.

Code Examples

Draw a dotted diagonal

#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    int i;
    initgraph(&gd, &gm, "");

    for (i = 0; i < 300; i += 4) {
        putpixel(100 + i, 80 + i, YELLOW);
    }

    getch();
    closegraph();
    return 0;
}

The output is a dotted yellow diagonal line. Each dot is one plotted pixel, spaced four pixels apart, which makes the individual coordinate steps easier to see.

Plot a simple sine wave

#include <graphics.h>
#include <conio.h>
#include <math.h>

int main() {
    int gd = DETECT, gm;
    int x, y;
    initgraph(&gd, &gm, "");

    setcolor(DARKGRAY);
    line(40, 240, 600, 240);
    line(40, 80, 40, 400);

    for (x = 0; x < 540; x++) {
        y = 240 - (int)(80 * sin(x * 3.14159 / 90));
        putpixel(40 + x, y, LIGHTCYAN);
    }

    getch();
    closegraph();
    return 0;
}

This program uses putpixel() to sample and draw a sine curve. The curve is not a built-in graphics.h shape; it appears because a loop calculates each coordinate and plots it.

Colored pixel grid

#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    int x, y, color;
    initgraph(&gd, &gm, "");

    for (y = 80; y < 240; y++) {
        for (x = 120; x < 520; x++) {
            color = 1 + ((x / 20 + y / 20) % 15);
            putpixel(x, y, color);
        }
    }

    getch();
    closegraph();
    return 0;
}

The nested loops paint a rectangular area pixel by pixel. The color formula creates repeating bands, which helps demonstrate that every pixel can be assigned independently.

Common Mistakes

Expecting setcolor() to control the pixel

putpixel() receives its own color argument. If you call setcolor(RED) but then call putpixel(x, y, YELLOW), the pixel is yellow.

Plotting too slowly in large loops

Single-pixel loops can be expensive. Keep animation loops tight, avoid unnecessary calculations inside nested loops, and draw only the pixels that changed when possible.

Using console-style coordinates

Graphics coordinates are pixel-based, not row-and-column text positions. A coordinate like (10, 10) is ten pixels from the top-left corner, not text row ten.

Try it online

Run the examples in the online compiler and adjust the loops to see how individual pixels build full drawings.