Short answer
On most common systems, a float takes 4 bytes, or 32 bits, and holds about 6 or 7 significant decimal digits. The C and C++ standards don’t set that exact size, though. The compiler and platform decide it, so check it with sizeof when it matters.
Why the standard doesn’t fix the size
The standards describe what a float must be able to do, not how many bytes it uses. The main rule is that double must provide at least as much precision as float, and long double at least as much as double.
Like the other primitive data types in c, float gets its size from the platform. Most current compilers on common hardware use the IEEE 754 single-precision format for float, which is 4 bytes, so that is the size you’ll usually see.
Checking the size with sizeof
#include <iostream>
int main() {
std::cout << "float: " << sizeof(float) << " bytes\n";
std::cout << "double: " << sizeof(double) << " bytes\n";
}
On a typical desktop, this prints 4 bytes for float and 8 bytes for double. sizeof returns a std::size_t, which std::cout prints as a plain number.
In C, print the result with %zu:
#include <stdio.h>
int main(void) {
printf("%zu\n", sizeof(float)); // usually 4
return 0;
}
For how the operator itself works, see the sizeof function in c.
What 4 bytes means for precision
Because a float holds about 6 or 7 significant digits, numbers that differ beyond that can end up stored as the same value. The number 16777216 is 2 to the power of 24. The next whole number, 16777217, can’t be stored in a float, so adding 1 changes nothing:
#include <iostream>
int main() {
float big = 16777216.0f; // 2^24
float next = big + 1.0f; // 16777217 can't be stored exactly
std::cout << std::boolalpha << (next == big) << '\n'; // true
}
If your calculations need more digits, use double, which holds about 15.
float, double and long double compared
| Type | Typical size | Significant decimal digits |
|---|---|---|
float |
4 bytes | About 6 to 7 |
double |
8 bytes | About 15 |
long double |
Depends on the platform | At least as many as double |
A float is less precise, but it uses half the memory of a double. That matters for large arrays.
Writing float values
A literal with a decimal point is a double unless you add an f suffix:
float a = 3.5f; // float literal
double b = 3.5; // double literal
Printing a float with printf
printf takes a variable number of arguments, so a float is promoted to double before the call. That means %f prints a float correctly:
#include <stdio.h>
int main(void) {
float ratio = 0.5f;
printf("%f\n", ratio); // 0.500000
return 0;
}
Key takeaways
- A
floatis usually 4 bytes with about 6 or 7 significant digits. - The standard sets minimum precision rules, and the platform sets the exact size.
- Check the size with
sizeofrather than assuming it. - Use
doublewhen afloatdoesn’t have enough digits.