Format Specifier in C Language in Hindi
Format specifier एक स्ट्रिंग है जिसका उपयोग formatted इनपुट और आउटपुट फ़ंक्शन में किया जाता है। format स्ट्रिंग इनपुट और आउटपुट के format को निर्धारित करता है। format स्ट्रिंग हमेशा ‘%’ वर्ण से शुरू होती है।
printf () फ़ंक्शन में आमतौर पर उपयोग किए जाने वाले format specifiers हैं:
Format specifier | Description |
%d or %i | इसका उपयोग signed integer value को print करने के लिए किया जाता है जहां हस्ताक्षर किए गए integer का अर्थ है कि variable positive और negative दोनों values को hold कर सकता है। |
%u | इसका उपयोग unsigned integer value को print करने के लिए किया जाता है जहां unsigned integer का अर्थ है कि variable केवल positive value रख सकता है। |
%o | इसका उपयोग octal unsigned integer को प्रिंट करने के लिए किया जाता है जहां ऑक्टल integer value हमेशा 0 value से शुरू होता है। |
%x | इसका उपयोग हेक्साडेसिमल unsigned integer को प्रिंट करने के लिए किया जाता है जहां हेक्साडेसिमल integer value हमेशा 0x value से शुरू होता है। इसमें वर्णमाला के अक्षर छोटे अक्षरों में print होते हैं जैसे a, b, c, आदि। |
%X | इसका उपयोग हेक्साडेसिमल unsigned integer को प्रिंट करने के लिए किया जाता है, लेकिन% X अपरकेस में वर्णमाला वर्णों को प्रिंट करता है जैसे A, B, C, आदि। |
%f | इसका उपयोग decimal floating-point values को प्रिंट करने के लिए किया जाता है। डिफ़ॉल्ट रूप से, यह ‘.’ के बाद 6 values को प्रिंट करता है। |
%e/%E | इसका उपयोग scientific notation के लिए किया जाता है। इसे Mantissa या Exponent के रूप में भी जाना जाता है। |
%g | इसका उपयोग decimal फ्लोटिंग-पॉइंट values को प्रिंट करने के लिए किया जाता है, और यह fixed precision का उपयोग करता है, अर्थात, decimal इनपुट के बाद का मूल्य आउटपुट में मूल्य के समान ही होगा। |
%p | इसका उपयोग हेक्साडेसिमल रूप में address को प्रिंट करने के लिए किया जाता है। |
%c | इसका उपयोग unsigned character को प्रिंट करने के लिए किया जाता है। |
%s | इसका उपयोग strings को प्रिंट करने के लिए किया जाता है। |
%ld | इसका उपयोग long-signed integer value को प्रिंट करने के लिए किया जाता है। |
आइए एक उदाहरण के माध्यम से format specifiers को विस्तार से समझते हैं।
- %d
- int main()
- {
- int b=6;
- int c=8;
- printf(“Value of b is:%d”, b);
- printf(“\nValue of c is:%d”,c);
- return 0;
- }
उपरोक्त कोड में, हम %d specifier का उपयोग करके b और c के पूर्णांक मान को print कर रहे हैं।
output
- %u
- int main()
- {
- int b=10;
- int c= -10;
- printf(“Value of b is:%u”, b);
- printf(“\nValue of c is:%u”,c);
- return 0;
- }
उपर्युक्त program में, हम unsigned format specifier का उपयोग करके b और c का मान प्रदर्शित कर रहे हैं, अर्थात,%u. B का मान positive है, इसलिए %u specifier b के सटीक मान को प्रिंट करता है, लेकिन यह c के मान को प्रिंट नहीं करता है क्योंकि c में negative मान होता है।
output
- %o
- int main()
- {
- int a=0100;
- printf(“Octal value of a is: %o”, a);
- printf(“\nInteger value of a is: %d”,a);
- return 0;
- }
उपरोक्त कोड में, हम octal value और integer value को प्रदर्शित कर रहे हैं।
output
- %x and %X
- int main()
- {
- int y=0xA;
- printf(“Hexadecimal value of y is: %x”, y);
- printf(“\nHexadecimal value of y is: %X”,y);
- printf(“\nInteger value of y is: %d”,y);
- return 0;
- }
उपरोक्त कोड में, y में हेक्साडेसिमल value ‘A’ है। हम दो format में y का हेक्साडेसिमल मान प्रदर्शित करते हैं। हम हेक्साडेसिमल मान को प्रिंट करने के लिए %x और %X का उपयोग करते हैं जहां %x छोटे अक्षरों में मूल्य प्रदर्शित करता है अर्थात ‘a’ और %X एक कैपिटल लेटर में मान को प्रदर्शित करता है अर्थात ‘A’
output
- %f
- int main()
- {
- float y=3.4;
- printf(“Floating point value of y is: %f”, y);
- return 0;
- }
उपरोक्त कोड y के floating value को प्रिंट करता है।
output
- %e
- int main()
- {
- float y=3;
- printf(“Exponential value of y is: %e”, y);
- return 0;
- }
output
- %E
- int main()
- {
- float y=3;
- printf(“Exponential value of y is: %E”, y);
- return 0;
- }
Output
- %g
- int main()
- {
- float y=3.8;
- printf(“Float value of y is: %g”, y);
- return 0;
- }
उपरोक्त कोड में, हम %g specifier का उपयोग करके y के floating value को प्रदर्शित कर रहे हैं। %g specifier आउटपुट को उसी precision के साथ इनपुट के रूप में प्रदर्शित करता है।
output
- %p
- int main()
- {
- int y=5;
- printf(“Address value of y in hexadecimal form is: %p”, &y);
- return 0;
- }
Output
- %c
- int main()
- {
- char a=’c’;
- printf(“Value of a is: %c”, a);
- return 0;
- }
Output
- %s
- int main()
- {
- printf(“%s”, “hinditutorialspoint”);
- return 0;
- }
Output
Minimum Field Width Specifier
मान लीजिए कि हम एक आउटपुट प्रदर्शित करना चाहते हैं जो स्क्रीन पर न्यूनतम संख्या में स्थान रखता है। आप format specifier के प्रतिशत चिन्ह के बाद integer संख्या प्रदर्शित करके इसे प्राप्त कर सकते हैं।
- int main()
- {
- int x=900;
- printf(“%8d”, x);
- printf(“\n%-8d”,x);
- return 0;
- }
उपरोक्त program में, %8d specifier 8 रिक्त स्थान के बाद मान प्रदर्शित करता है जबकि %-8d specifier मूल्य को left align करेगा।
output
अब हम देखेंगे कि खाली जगहों को कैसे भरें। यह नीचे दिए गए कोड में दिखाया गया है:
- int main()
- {
- int x=12;
- printf(“%08d”, x);
- return 0;
- }
उपरोक्त program में, %08d का अर्थ है कि खाली स्थान zero से भरा है।
output
Specifying Precision in hindi
हम ‘.’ (Dot) operator का उपयोग करके precision निर्दिष्ट कर सकते हैं जिसके बाद integer और format specifier होता है।
- int main()
- {
- float x=12.2;
- printf(“%.2f”, x);
- return 0;
- }
output