How floats are stored in C compiler?


In C programming language, float is a short term for floating point.

  • Floating point numbers are generally represented in the form of Institute of Electrical and Electronics Engineers (IEEE) format.

  • The IEEE format uses a sign bit, a mantissa and an exponent for representing the power of 2.

  • The sign bit denotes the sign of the number: a 0 represents a positive value and a 1 denotes a negative value.

  • The mantissa represented in binary after converting into its normalized form. After normalization mantissa, the most significant digit is always 1.

  • The exponent is an integer stored in unsigned binary format after adding a positive integer bias.

  • This ensures that the stored exponent is always positive.

  • For float, the bias is 127 and for doubles, the bias is 1023.

Example

Following is the C program for rounding the floating point number to four decimal places in C language −

 Live Demo

#include <stdio.h>
int main(){
   float var = 37.66666;
   printf("%.4f", var);// rounding to four decimal points
   return 0;
}

Output

When the above program is executed, it produces the following result −

37.6667

Following is the C program for rounding floating point number to eight decimal places in C language −

Program

 Live Demo

#include <stdio.h>
int main(){
   float var = 78.67;
   printf("%.8f", var);
   return 0;
}

Output

When the above program is executed, it produces the following result −

78.66999817

Updated on: 13-Mar-2021

626 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements