Division with integer operands always produces an integer result.
Division with floating-point operands will give you an exact result.
The next example illustrates how division operations work with variables of type float.
#include <stdio.h> int main(void) { float wood_length = 10.0f; // In feet float table_count = 4.0f; // Number of equal pieces float table_length = 0.0f; // Length of a piece in feet table_length = wood_length/table_count; printf("A wood %f feet long can be cut into %f pieces %f feet long.\n", wood_length, table_count, table_length); return 0;// www .ja v a 2 s .c o m }
You use a new format specifier for values of type float in the printf() statement:
printf("A plank %f feet long can be cut into %f pieces %f feet long.\n",
wood_length, table_count, table_length);
The format specifier %f can display floating-point values.
The format specifier that you use must correspond to the type of value you're outputting.