Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

The % Format Specifiers in C Programming

4 min 47 sec read

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.
    
    

Recommended Articles

1) Write C programs to implement i) strcpy(), ii) strlen() library function by yourself

2) Write C program to insert a node after a given node in a linked list

3) How to use %5.2f format specifier in C Programming?

4) Question on C Program referencing the Increment and Decrement Operators concept asked in GATE CSE

5) Article on different storage classes in C.

6) C Tutorial on Calloc() vs malloc() functions is explained in depth in this article.

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor