In C, the float data type represents floating point numbers, using 32 bits. We use this type more often than the double, because we rarely need the double’s precision.
float price = 5.50f;
printf("The current price is %f.", price);
A float value normally ends with the letter ‘f’. If we leave it out the literal(5.50) will be treated as double by default. Further, you see that the specifier for printing floats is %f.
The specifier for printing and scanning float values is the same %f.
We can specify the precision of the output. To do this, we add the decimal delimiter ‘.’, followed by a number. This is the number of digits after the decimal mark that will be used as a precision. When the actual value has longer precision, it will be rounded for the printing purposes.
In the next example (%.2f) we will print the value with precision two digits after the decimal mark.
float balance = 1000.55753f;
printf("Your balance: $%.2f\n", balance);
printf("How much would you like to withdraw?");
if(amount > 0 && amount <= balance)
balance = balance - amount;
printf("The new balance is $%.2f", balance);
Most C compilers use the IEEE 754 standard for encoding the float values. According to it, the single precision (float) is represented by 32 bits as follows:
See also: double data type