Parameters
| Name | Type | Description |
|---|---|---|
x | int | The x-coordinate where the text reference point is placed. Larger values move text toward the right. |
y | int | The y-coordinate where the text reference point is placed. Larger values move text downward on the screen. |
textstring | const char * | The null-terminated string to draw. It can be a string literal, a character array, or text built with sprintf(). |
The exact visual position depends on settextjustify(). With the default left and top alignment, x and y mark the upper-left starting point of the text. If you change justification to center text, the same coordinate becomes the center anchor. That makes it possible to center labels inside rectangles or under circles without manually calculating the full string width.
Return Value
outtextxy() returns void. It does not report whether the text fit on the screen. If text appears clipped, check the coordinate, text size, text direction, and viewport. For layout-sensitive programs, use textwidth() and textheight() before drawing so you can position the string within a panel or around a shape.
Code Examples
Basic text label
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(YELLOW);
outtextxy(120, 100, "Hello Graphics!");
getch();
closegraph();
return 0;
}
This program draws one yellow string near the upper-left area of the graphics screen. The output is simple, but it shows the most important rule: text appears at the graphics coordinate, not in the console window.
Label a rectangle panel
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(WHITE);
rectangle(140, 110, 500, 260);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
setcolor(LIGHTCYAN);
outtextxy(320, 185, "Graphics Menu");
getch();
closegraph();
return 0;
}
Here the text is centered inside a rectangle by changing text justification. This is a common pattern for menus, dialog boxes, title cards, and simple user interface mockups.
Print changing numeric values
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
int radius = 90;
char label[40];
initgraph(&gd, &gm, "");
setcolor(WHITE);
circle(320, 220, radius);
sprintf(label, "Radius = %d pixels", radius);
setcolor(LIGHTGREEN);
outtextxy(230, 340, label);
getch();
closegraph();
return 0;
}
This example builds a text string at runtime with sprintf(). It is useful for showing scores, coordinates, distances, iteration counts, and live values in animation demos.
Common Mistakes
Using printf() instead of graphics text
printf() writes to the console, while outtextxy() writes to the graphics screen. In graphics mode, use BGI text functions when the label must appear inside the drawing area.
Forgetting active color and text style
The function does not accept color, size, or font arguments. Set those with setcolor(), settextstyle(), and settextjustify() before calling it.
Placing text outside the viewport
Large x or y values can place the label off-screen. Large fonts can also extend beyond a panel even when the anchor point is inside it.
Try it online
Paste an example into the online compiler and change the coordinates, text size, and justification to see how BGI text placement behaves.