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

C के Format Specifiers

Format specifiers %d और %f जैसे छोटे placeholders हैं जो C को बताते हैं कि किसी spot में किस तरह की value जाती है, एक form में labeled blanks की तरह। हर एक एक type से match करता है।
Syntax
c
printf("%d", integer_value);
printf("%f", float_value);
printf("%c", char_value);
printf("%s", string_value);

Format Specifiers क्या हैं?

एक format specifier printf या scanf के format string के अंदर एक placeholder है जो function को बताता है कि उस position पर किस data type की उम्मीद है -- इसे गलत करने से हमेशा program crash नहीं होता, लेकिन यह value को corrupt या misread करता है।

उदाहरण: What are Format Specifiers?

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;
}

Integers के लिए Specifiers (%d और %i)

%d और %i दोनों एक signed base-10 integer format करते हैं और printf में interchangeable हैं, हालांकि %i scanf conventions में थोड़ा ज़्यादा common है -- किसी भी तरह, value actually एक int-sized integer होनी चाहिए, float या long नहीं।

उदाहरण: Specifiers for Integers (%d and %i)

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

Decimals के लिए Specifiers (%f और %lf)

%f एक float print या read करता है, जबकि %lf specifically scanf से एक double पढ़ते समय ज़रूरी है (printf doubles के लिए दोनों interchangeably accept करता है क्योंकि C float arguments को कैसे promote करता है) -- scanf के साथ इन्हें मिलाना आपका data corrupt कर देगा।

उदाहरण: Specifiers for Decimals (%f and %lf)

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

Text के लिए Specifiers (%c और %s)

%c exactly एक character पढ़ता या print करता है, जबकि %s एक पूरी string (characters का एक null-terminated array) handle करता है -- जहाँ आपका मतलब %s था वहाँ %c इस्तेमाल करना सिर्फ पहला letter capture करेगा और बाकी input unread छोड़ देगा।

उदाहरण: Specifiers for Text (%c and %s)

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

Addresses के लिए Specifiers (%p)

%p एक pointer की value को इसके raw memory address की तरह format करता है, आमतौर पर hexadecimal में दिखाया जाता है -- मुख्य रूप से debugging के लिए useful, यह confirm करने के लिए कि एक variable memory में actually कहाँ रहता है या दो pointers same location reference करते हैं।

उदाहरण: Specifiers for Addresses (%p)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%p", (void*)&x);
	// 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. एक float या double के लिए %d इस्तेमाल करना, जो गलत values print करता है।
  2. scanf में एक double के लिए %lf की बजाय %f इस्तेमाल करना।
  3. एक long को %ld की बजाय %d से print करना।
🔒

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.