Float Data Type in C

This Float Data Type in C  post explains clearly about float data type in c language with limits and suitable examples.


 

Float data types:

  • A floating point type variable is a variable that can hold a real number, such as 4.0, 2.5, 3.33, or 0.1226.
  • There are three different floating point data types: float, double, and long double.
  • These can have positive or negative values:

 

float: The basic floating-point type for the system; it can represent at least six significant figures accurately.

 

double: A (possibly) larger unit for holding floating-point numbers. It may allow more significant figures (at least 10, typically more) and perhaps larger exponents than float.

 

long double: A (possibly) even larger unit for holding floating-point numbers. It may allow more significant figures and perhaps larger exponents than double.

 

#include <stdio.h>
main()
{
float a = 1.1;
double b = 1.1;
if(a==b)
printf(“Same”);
else
printf(“Different”);
}

 

#include<stdio.h>
int main()
{
float a=0.7;
if(a < 0.7)
printf(“C\n”);
else
printf(“C++\n”);
return 0;
}

 

  • For floating point numbers (float, double, long double) the values cannot be predicted exactly.
  • Depending on the number of bytes, the precision with of the value represented varies.
  • Float takes 4 bytes and double takes 10 bytes. So float stores 0.9 with less precision than long double.
  • In scientific notation, numbers are represented as decimal number times powers of 10.
  • Here are some examples.

Number                            Scientific Notation                           Exponential Notation
1,000,000,000                       1.0×109                                                 1.0e9
123,000                                     1.23×105                                               1.23e5
322.56                                       3.2256×102                                         3.2256e2
0.000056                                 5.6×10-5                                               5.6e–5

Share this