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
Float and Double in C
In C programming, float and double are data types used to represent floating point numbers. Both follow the IEEE 754 standard but differ in precision and memory usage.
Float Data Type
Float is a 32-bit IEEE 754 single precision floating point number with the following structure −
- 1 bit for the sign
- 8 bits for the exponent
- 23 bits for the mantissa (fractional part)
- Precision: 6-7 decimal digits
Syntax
float variable_name;
Example: Float Declaration and Usage
#include <stdio.h>
int main() {
float x = 10.327f;
int y = 28;
printf("The float value: %.6f
", x);
printf("The sum of float and int: %.6f
", (x + y));
return 0;
}
The float value: 10.327000 The sum of float and int: 38.327000
Double Data Type
Double is a 64-bit IEEE 754 double precision floating point number with the following structure −
- 1 bit for the sign
- 11 bits for the exponent
- 52 bits for the mantissa
- Precision: 15-17 decimal digits
Syntax
double variable_name;
Example: Double Declaration and Usage
#include <stdio.h>
int main() {
float x = 10.327f;
double y = 4244.546789123456;
int z = 28;
printf("The float value: %.6f
", x);
printf("The double value: %.15lf
", y);
printf("The sum of all variables: %.6f
", (x + y + z));
return 0;
}
The float value: 10.327000 The double value: 4244.546789123456000 The sum of all variables: 4282.873789
Comparison
| Property | Float | Double |
|---|---|---|
| Size | 32 bits (4 bytes) | 64 bits (8 bytes) |
| Precision | 6-7 decimal digits | 15-17 decimal digits |
| Format Specifier | %f | %lf (input), %f (output) |
| Suffix | f (e.g., 3.14f) | None required |
Conclusion
Use float when memory is a concern and moderate precision is sufficient. Choose double for applications requiring high precision calculations, as it provides nearly twice the precision of float with minimal performance overhead on modern systems.
Advertisements
