← Back to C Course | Chapter 2: Input & Output | Lesson 1 of 7

C का printf()

printf वह तरीका है जिससे एक C program screen पर messages और values दिखाता है, उन्हें announce करने वाले एक loudspeaker की तरह। आप text में placeholders डालते हैं और यह उन्हें भर देता है।
Syntax
c
printf("format string");

printf("text %specifier", variable_name);

printf() क्या है?

printf ("print formatted") screen पर text और variable values लिखने के लिए standard C library function है -- यह stdio.h का हिस्सा है और वह primary तरीका है जिससे एक C program इसे चलाने वाले व्यक्ति को कुछ भी वापस बताता है।

उदाहरण: What is printf()?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Hello from printf");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Variables Print करना

एक printf string के अंदर %d या %s जैसे format specifiers placeholders की तरह काम करते हैं जो order में, format string के बाद आपके pass किए गए arguments की values से replace हो जाते हैं -- specifier और variable के type को mismatch करना garbage output produce करता है।

उदाहरण: Printing Variables

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Print formatted text to the screen
	printf("%d", age);
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Values Print करना

जब printf को कई format specifiers के साथ call किया जाता है, हर एक list में left-to-right order में अगला argument consume करता है, इसलिए printf("%d and %d", a, b) पहले a की value print करता है और दूसरी b की, उनकी position से match करते हुए।

उदाहरण: Printing Multiple Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 1, b = 2;
	// Print formatted text to the screen
	printf("%d and %d", a, b);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Output में Newlines इस्तेमाल करना

क्योंकि printf खुद कभी line break add नहीं करता, consecutive printf calls same line पर एक-दूसरे के तुरंत बाद print होते हैं जब तक आप explicitly \n शामिल न करें जहाँ भी आप चाहते हैं कि output एक नई line पर जाए।

उदाहरण: Using Newlines in Output

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("First\n");
	// Print formatted text to the screen
	printf("Second");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Floating-Point Precision Format करना

%f की बजाय %.2f लिखना print होने पर एक floating-point value को exactly 2 decimal places तक round करता है -- currency जैसी चीज़ें decimal point के बाद C के default six digits की बजाय cleanly दिखाने के लिए essential।

उदाहरण: Formatting Floating-Point Precision

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare price and set it to 19.9999
	double price = 19.9999;
	// Print formatted text to the screen
	printf("%.2f", price);
	// Return 0 to signal that the program finished successfully
	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. गलत format specifier इस्तेमाल करना जैसे एक float के लिए %d, जो garbage print करता है।
  2. printf को format specifiers से कम arguments देना, जो undefined behavior है।
  3. एक user-supplied string को format की तरह pass करना, जैसे printf(input);, जो एक security risk है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.