position: fixed; top: auto !important; margin-left: 112px;

% Format specifiers in C

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.
    
    

Solve the Quiz of Article

1) What will be the output of following printf() statement? Assume all libraries are included.
int a = 12;
printf(“a = %3d”, a);
_ _ _12
_12
where _ is space.

2) What do you think the output of following will be? Assume all libraries are included.
float a = 1.289999;
printf(“ a = %5.2f ”, a);
_1.29
_ _ _ _ _ .1289



Previous Next Article








codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.