Format specifier in C with Example - C programming में Format specifier
C

Format specifier in C with Example – C programming में Format specifier क्या होता है

Format specifier in C with Example – C programming में Format specifier क्या होता है? C programming के tutorial series में हमने पिछले tutorial में सीखा था की C programming में constant क्या होता है? आज के इस tutorial में हम सीखेंगे की C programming में format specifier क्या होता है? C programming में data को represent करने के लिए हमें format specifier की आवश्यकता पडती है|

Format specifier in C with Examples in Hindi

Format specifier C programming में data को represent करने का एक तरीका है जो compiler को यह बताता है की इस variable में किस प्रकार का value store है या फिर किस प्रकार का value store होगा|

चलिए example से समझते हैं| मान लीजिये आप एक गैस की दुकान पर जा रहे हैं और आपने अपने हाथ में गैस की टंकी पकड़ी हुयी है तो दुकानदार को यह समझ आ जायेगा की आपको गैस भरवाना है क्योंकि आप गैस की टंकी में चावल नहीं ले सकते| ठीक उसी प्रकार C programming में अलग अलग प्रकार के data को represent करने के लिए कुछ character को formatting किया गया है जिसे format specifier कहा जाता है|

Format specifier ज्यादातर printf() और scanf() function में use किये जाते हैं| Format specifier % operator के साथ start होता है और % operator के बाद कुछ character add होते हैं जो की data के type को represent करते हैं|For Example: %d, %c, %f. Format specifier को format string भी कहा जाता है|

List of format specifier

C programming में ढेर सारे format specifier हैं जो की अलग अलग प्रकार के data को represent करने के लिए इस्तेमाल होते हैं|

Table credit goes to codeforwin.org
Format SpecifierSupported Data TypesDescription
%cchar
unsigned char
Character
%dshort
unsigned short
int
long
Signed Integer
%e or %Efloat
double
Scientific notation of float values
%ffloatFloating point
%g or %Gfloat
double
Similar as %e or %E
%hishortSigned Integer(Short)
%huunsigned shortUnsigned Integer(Short)
%ishort
unsigned short
int
long
Signed Integer
%l or %ld or %lilongSigned Integer
%lfdoubleFloating point
%Lflong doubleFloating point
%luunsigned int
unsigned long
Unsigned integer
%lli, %lldlong longSigned Integer
%lluunsigned long longUnsigned integer
%oshort
unsigned short
int
unsigned int
long
Octal representation of Integer.
%pvoid *Address of pointer to void void *
%schar *String
%uunsigned int
unsigned long
Unsigned Integer
%x or %Xshort
unsigned short
int
unsigned int
long
Hexadecimal representation of Unsigned Integer
%nPrints nothing
%%Prints % character

printf() और scanf() दोनों में ही format specifier लगभग same होते हैं लेकिन दोनों में थोडा सा difference होता है तो चलिए जानते हैं की क्या difference होता है| printf() function में Format specifier का इस्तेमाल variable के value को print करने के लिए होता है जबकि scanf() में format specifier का इस्तेमाल variable में value को store करने के लिए होता है यानि की keyboard के through इनपुट लेने के लिए होता है|

कभी कभी आपको simple format specifier के जगह कुछ extra character के साथ format specifier मिलेंगे जैसे की %2d, %-2s etc. चलिए जानते हैं इसके बारे में:

  • – (minus) sign left alignment के लिए इस्तेमाल किया जाता है जैसे मान लीजिये की कोई भी output right की तरफ print हो रही है और आप उसे left की तरफ print करना चाहते हैं तो उसके लिए – sign का इस्तेमाल कर सकते हैं|
  • % operator के बाद जो number रहता है वह ये minimum width को define करता है जैसे मान लीजिये आपके पास 3 digit का एक number है और दूसरा number 5 digit का है और आप दोनों number को एक सम्मान लिखना चाहते हैं ताकि दोनों को आसानी से calculate कर सकें तो आप %5d का इस्तेमाल कर सकते हैं| यदि आप % operator के बाद minimum width 5 define करते हैं और आपके पास 2 या 3 digit का number है तो वह left side से 3 space या 2 space extra add कर लेगा|
  • % operator और number के बीच अगर 0 add होता है तो वह minimum width के अनुसार space के जगह पर 0 add कर लेता है जैसे यदि किसी variable में दो digit का number है और यदि आप 5 digit maximum width (%5d) define करते हैं तो यह 2 digit के पहले तीन जीरो add कर लेगा| For example: int a = 22; prinft(“%05d”,%a); output: 00022
  • point (.) symbol अलग अलग प्रकार के data type में अलग अलग कार्यो के लिए इस्तेमाल होता है जैसे यदि आप point symbol को string data को print करने के लिए इस्तेमाल करते हैं तो यह केवल उतना ही character को print करेगा जितना आप point symbol के बाद number देंगे जैसे %.2s यह केवल दो character को ही print करेगा| जबकि floating point के case में यह दशमलव के बाद उतना digit print करेगा जितना number दिया हुआ होगा| For example: %.2f यह दशमलव के बाद दो digit ही print करेगा| point symbol maximum width को represent करता है|

चलिए अब programming के द्वारा कुछ format specifier के example देखते हैं|

Character Format Specifier %c

#include <stdio.h>

int main() 
{ 
    char ch = 'G'; 
    printf("%c", ch); 
    return 0; 
} 

Output

G

Integer Format specifier %d, %i, %u

#include <stdio.h>

int main() 
{ 
    int a = 20, b = 30 , c = 35;
    printf("%d\n", a); 
    printf("%i\n", b); 
    printf("%u\n", c); 
    return 0; 
} 

Output

20
30
35

Float and double format specifier %f, %e, %lf, %Lf

#include <stdio.h>

int main() 
{ 
    float f = 20.35;
    double d = 203.200;
    long double ld = 20144.110;
    printf("%f\n", f); 
    printf("%e\n", f); 
    printf("%lf\n", d); 
    printf("%Lf\n", ld);
    return 0; 
} 

Output

20.350000
2.035000e+01
203.200000
20144.110000

String Printing %s

#include <stdio.h>

int main() 
{ 
    char str[] = "Guptatreepoint";
    printf("%s\n", str); 
    return 0; 
}

Output

Guptatreepoint

Octal and Hexadecimal For integer %o, %x, %X

#include <stdio.h>

int main() 
{ 
    int a = 45; 
    printf("Octal values = %o\n", a); 
    printf("Hexadecimal Values = %x\n", a); 
    return 0; 
}  

Output

Octal values = 55
Hexadecimal Values = 2d

Extra Formatting Format specifier

#include <stdio.h>

int main() 
{ 
    char ch[] = "Guptatreepoint"; 
    int a = 25;
    printf("%30s\n", ch); 
    printf("%-30s\n", ch); 
    printf("%30.5s\n", ch); 
    printf("%-30.5s\n", ch); 
    printf("%05d\n", a);
    return 0; 
} 

Output

                Guptatreepoint Guptatreepoint      
               Gupta 
Gupta                         
00025

ये भी पढ़ें:

C programming में Data type क्या होता है?

Constant in C programming

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.