Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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
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.
Advertisements
