The % Format Specifiers in C Programming

4 min 47 sec read Basic


Format specifiers in C are used in printf() & scanf() functions to determine the datatype of printing value.

Float Format Specifier

Everything you should know about float format specifier is given below,

Example :-

If you want to print float value, program will be look like this
    a = 1.12
    printf(“ a = %f ”, a );
    
    Output :-
    a = 1.12

Here, %f :- is format specifier for float types.

Example :-

    a = 1
    printf(“ a = %f ”, a );
    
    Output :-
    a = 1.000000

Here,

you assigned a = 1, but used %f so compiler will recognize it as a float value so output will be in decimal value which is 1.000000

Similarly,

For double format specifier is %lf
    For String format specifier is %s
    For integer format specifier is %d

Now the output of float and double is too long if you have observed in theabove example. Output was a = 1.0000000

Float Output Format

You can restrict the decimal values after dot like this,

float a = 3.122222223;
    printf(“a = %.2f ”, a);
    
    Output :- 
    a = 3.12

%.2f will restrict the values upto two decimal values.

    Example :-
    %.3f will restrict the values upto three decimal values.
    float a = 3.122222223;
    printf(“a = %.3f ”, a);
    
    Output:-
     a = 3.122

One more format specifier is mostly asked in interview questions ,

    Example:-
    float a = 1.289999;
    printf(“ a = %6.2f ”, a);
    
    Output:-
    _ _1.29 where _ are spaces 

%6.2f :- means output will be in 6 columns

Here ,

a = 1.28999;

So after applying %6.2f it will print two spaces before 1 and .2f means two digits after dot. Include dot also in count it also requires space to get stored.

Example :-

    float a = 1.289999;
    printf(“ a = %5.3f ”, a);
    

Output :-

Here ,

%5.3f :- prints answer in 5 columns .
             three digits after dot as there is .3f 
             a 1 before dot . 
    Include dot also in the count it also requires space to get stored.
    

Example:-

float a = 1.289999;
    printf(“ a = %5.2f ”, a);

Output:-

Here,

%5.2f :- prints answer in 5 columns.
             two digits after dot as there is .2f
             and space and 1 before dot.

Example:-

    float a = 1111.289999;
    printf(“ a = %5.3f ”, a);

Output :-

a = 1111.290

Here,

Before dot it contains enough digits than 5 so it will store 1111 in one block and rest in another.

Space will only be added when there are not enough digits.

Quick Recap:-

Question

printf(“ %5.2f “, a);

Answer

    If a is 1111.2344444, it prints 1111.23
    If a is 11.2344444, it prints 11.23
    If a is 1.2344444 , it prints _1.234 ,
    where _ is one leading whitespace character.

All about integers

Example:-

If you want to print an integer value

    a = 1,
    printf(“a =  %d ”, a );

Here,

%d :- is format specifier for integer types.

Output:-

a = 1

Example:-

Even if you give float value to int like,

 

int a = 1.12;
    printf(“ a = %d ”, a);

 

Output :- a = 1

Since you used %d in printf(), compiler will recognise it as an integer value . So it skips decimal value .12 from 1.12.

Integer Output Format

int a = 1234;
    printf(“a = %3d”, a);
    Output :-
    a = 1234
    int a = 123;
    printf(“a = %3d”, a);
    Output :-
    a = 123
    
    int a = 12;
    printf(“a = %3d”, a);
    Output :-
    a = _12
    where, _ is space
    
    int a = 1;
    printf(“a = %3d”, a);
    Output :-
    a = _ _1
    where, _ are spaces.
    
    

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python