The % Format Specifiers in C Programming
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,
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
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
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.
All about integers
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.
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.
1:1 & 1:Many Mentorship Program
Be job-ready. Learn how to crack the interviews with industry ready training program.
Data Structures & Algorithms
What is Dynamic Programming and how to use it
This is a quick introduction to Dynamic Programming.
3 ways to check anagrams
Let's discuss the 3 ways to check anagrams and their time complexity.
Bubble Sort Algorithm
Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity
Introduction to Trees
What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node
Address Calculation in 2d array
Let's discuss about the Address Calculation using row-major order & column-major order.
Let's discuss the difference between postfix and prefix with a practical example.
Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo
Let's discuss the Modal Popups in Bootstrap.
How to create a dropdown list in navbar bootstrap
In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.
Navigation tabs in Bootstrap
Let's discuss about the navigation tabs in Bootstrap
Let's discuss about the button classes in bootstrap.