How floats are stored in C compiler?

In C programming language, float is a short term for floating point. Understanding how floats are stored internally helps developers work with precision and memory optimization effectively.

IEEE 754 Standard

Floating point numbers are represented using the IEEE 754 standard format, which consists of three components −

  • Sign bit: 1 bit that denotes the sign (0 for positive, 1 for negative)
  • Exponent: 8 bits for float (11 bits for double) stored with a bias
  • Mantissa (Significand): 23 bits for float (52 bits for double)

Syntax

float variable_name = value;
// IEEE 754 format: [Sign][Exponent][Mantissa]
// Float: 1 + 8 + 23 = 32 bits total

Memory Layout Visualization

Sign 1 bit Exponent 8 bits Mantissa (Significand) 23 bits IEEE 754 Single Precision Float (32 bits) Bit 31 Bits 30-23 Bits 22-0

Key Storage Details

  • The exponent uses a bias of 127 for float (1023 for double)
  • The mantissa is normalized with an implicit leading 1
  • Total storage: 32 bits for float, 64 bits for double

Example 1: Float Precision Demonstration

This example shows how float precision affects decimal representation −

#include <stdio.h>

int main() {
    float var = 37.66666f;
    
    printf("Original value: 37.66666
"); printf("Rounded to 4 decimals: %.4f
", var); printf("Full precision: %.10f
", var); return 0; }
Original value: 37.66666
Rounded to 4 decimals: 37.6667
Full precision: 37.6666603088

Example 2: Precision Loss in Float Storage

This demonstrates how binary representation affects decimal precision −

#include <stdio.h>

int main() {
    float var = 78.67f;
    
    printf("Expected: 78.67
"); printf("Stored as: %.8f
", var); printf("Actual binary representation affects precision
"); return 0; }
Expected: 78.67
Stored as: 78.66999817
Actual binary representation affects precision

Memory Size Comparison

Type Size (bytes) Sign Bits Exponent Bits Mantissa Bits Bias
float 4 1 8 23 127
double 8 1 11 52 1023

Conclusion

Float storage in C follows IEEE 754 standard with 32-bit representation using sign, exponent, and mantissa components. Understanding this format explains precision limitations and helps in choosing appropriate data types for numerical computations.

Updated on: 2026-03-15T13:13:44+05:30

991 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements