C के Format Specifiers
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक
floatयाdoubleके लिए%dइस्तेमाल करना, जो गलत values print करता है। scanfमें एकdoubleके लिए%lfकी बजाय%fइस्तेमाल करना।- एक
longको%ldकी बजाय%dसे print करना।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: