Results can only be achieved if you have the focused direction and crystal clear knowledge. To achieve this, you need a mentor. We will help you out by connecting with an expert mentor in the field.Learn More
4 min 47 sec read
Format specifiers in C are used in printf() & scanf() functions to determine the datatype of printing value.
Everything you should know about float format specifier is given below,
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.
a = 1 printf(“ a = %f ”, a ); Output :- a = 1.000000
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
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
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
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.
float a = 1.289999; printf(“ a = %5.3f ”, a);
%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.
float a = 1.289999; printf(“ a = %5.2f ”, a);
%5.2f :- prints answer in 5 columns. two digits after dot as there is .2f and space and 1 before dot.
float a = 1111.289999; printf(“ a = %5.3f ”, a);
a = 1111.290
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.
printf(“ %5.2f “, a);
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.
If you want to print an integer value
a = 1, printf(“a = %d ”, a );
%d :- is format specifier for integer types.
a = 1
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.
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.
Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)