Skip to main content
Logo image

Section 4.10 The float Datatype

Storing decimal numbers takes up more computer memory than storing integers because of all those decimal places. The more precision you are looking for the more space it takes up. Although today’s computers come with plenty of memory, it is prudent to only use up what you really need.
C has several data types to store decimal numbers: float, double and long double. They have increasing precision. We’ll mostly use the type float.
The format specifier for a float is %f.
While internally, numbers are always stored with the precision dictated by the number’s data type, for visualization purposes you can specify the number of decimal places to display using %.1f (one decimal place), %.2f (two decimal places), etc.
By default (if using simply %f) floats are displayed with 6 decimal places.

admin.....open in new window

Investigate 4.10.

What happens if the precision of the format specifier exceeds that of the number being displayed? For example, if the user enters 1.77 in the code window above, but the code specifies the output to have four decimal places, %.4f, what will happen?
Hint.
Try it out above to see what happens, then see if you can guess why that happens!