Parameters
| Name | Type | Description |
|---|---|---|
x | int | The horizontal pixel coordinate. In standard VGA-like modes, 0 is the left edge. |
y | int | The vertical pixel coordinate. In graphics.h, 0 is the top edge and larger values move downward. |
color | int | The 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.