C float type
What are floating-point values
C provides two main floating-point representations:
- float (single precision)
- double (double precision).
The numbers 5.5, 8.3, and -12.6 are all floating-point numbers. Floating-point numbers must contain a decimal point. So 5.0 is a floating-point number, while 5 is an integer.
Range for float
Type | Range |
---|---|
float | +/-3.4E38 to +/-3.4E38 |
A floating-point number has a fractional part and a biased exponent. Float occupies 4 bytes and double occupies 8 bytes.
Example - float type
The following code define two variables, one int and one float variable and output to the console.
#include <stdio.h>
// w ww .j a v a 2 s. c o m
int main()
{
float price = 1.45;
printf("That be $%f, please.\n",price);
return(0);
}
The code above generates the following result.
Example - divide two floats
When using a floating-point number in printf, the %f conversion is used. To print the expression 1.0/3.0, we use this statement:
#include<stdio.h>
/* w w w .ja v a 2 s . com*/
int main(void){
printf("The answer is %f\n", 1.0/3.0);
}
The code above generates the following result.