← Back to C Course | Chapter 13: Graphics | Lesson 6 of 10

C Graphics में Text

Graphics में text का मतलब है अपनी drawing पर words और numbers लिखना, एक picture में labels add करने जैसा। आप font और position चुन सकते हैं।
Syntax
c
outtext("text");                  // at the current position
outtextxy(x, y, "text");           // at (x, y)
settextstyle(font, direction, size);
settextjustify(horizontal, vertical);

Text Output करना

outtext() graphics system की current cursor position से शुरू होते हुए एक text string print करता है, जबकि outtextxy() इसकी बजाय आपको exact (X, Y) pixel coordinates specify करने देता है जहाँ text draw होना चाहिए, cursor को manually track किए बिना precise placement देते हुए।

उदाहरण: Outputting Text

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	outtextxy(100, 100, "Hello");
	getch();
	closegraph();
	return 0;
}

Fonts और Text Styles

settextstyle() font, size, और orientation बदलता है जो बाद के text output में इस्तेमाल होता है, normal left-to-right text के लिए HORIZ_DIR और vertically पढ़ने के लिए rotated text के लिए VERT_DIR जैसे orientations support करते हुए, जो एक chart के vertical axes label करने के लिए handy है।

उदाहरण: Fonts and Text Styles

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
	outtextxy(50, 50, "Styled");
	getch();
	closegraph();
	return 0;
}

Text Alignment

settextjustify() control करता है कि text आपके दिए coordinate के relative कैसे align होता है, अलग horizontal (LEFT_TEXT, CENTER_TEXT, RIGHT_TEXT) और vertical justification constants accept करते हुए, इसलिए same outtextxy() call एक fixed point के around text center, left-align, या right-align कर सकती है।

उदाहरण: Text Alignment

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	settextjustify(CENTER_TEXT, CENTER_TEXT);
	outtextxy(150, 150, "Centered");
	getch();
	closegraph();
	return 0;
}

Numeric Values Display करना

क्योंकि outtext() और outtextxy() सिर्फ plain string arguments accept करते हैं, एक numeric value display करने के लिए पहले उस number को खुद एक string में convert करना होगा, आमतौर पर एक character buffer में लिखने वाले sprintf() से, उस buffer को text-drawing function को pass करने से पहले।

उदाहरण: Displaying Numeric Values

c
#include <graphics.h>
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	initgraph(0, 0, "");
	char buffer[10];
	sprintf(buffer, "%d", 42);
	outtextxy(100, 100, buffer);
	getch();
	closegraph();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Text और Shapes मिलाना

positioned text को drawn shapes के साथ combine करना — जैसे एक centered label के पीछे एक rectangle — BGI drawing primitives से पूरी तरह menus, labeled buttons, और dashboards जैसे simple graphical interfaces बनाने के पीछे basic technique है।

उदाहरण: Mixing Text and Shapes

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	rectangle(50, 50, 150, 100);
	outtextxy(70, 70, "Label");
	getch();
	closegraph();
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. graphics mode में printf इस्तेमाल करना और window में text की उम्मीद करना, जब आपको outtextxy इस्तेमाल करना चाहिए।
  2. outtextxy को पहले sprintf से string में convert किए बिना एक number pass करना।
  3. text draw करने के बाद text style set करना, ताकि इसका कोई असर न हो।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.