0
0
CComparisonBeginner · 3 min read

Int vs Float vs Double in C: Key Differences and Usage

In C, int stores whole numbers without decimals, float stores single-precision decimal numbers, and double stores double-precision decimal numbers with more accuracy. Use int for integers, float for less precise decimals, and double when you need more decimal precision.
⚖️

Quick Comparison

Here is a quick table comparing int, float, and double in C based on key factors.

Factorintfloatdouble
Type of DataWhole numbers (integers)Decimal numbers (single precision)Decimal numbers (double precision)
Size (typical)4 bytes4 bytes8 bytes
PrecisionExact for integersApprox. 6-7 decimal digitsApprox. 15-16 decimal digits
Range-2,147,483,648 to 2,147,483,647±1.5 × 10⁻⁴⁵ to ±3.4 × 10³⁸±5.0 × 10⁻³²⁴ to ±1.7 × 10³⁰⁸
Use CaseCounting, indexingLess precise decimalsHigh precision decimals
Memory UsageLessModerateMore
⚖️

Key Differences

int is used to store whole numbers without any decimal part. It is exact for integers within its range and uses less memory compared to floating-point types.

float stores decimal numbers but with limited precision (about 6-7 digits). It uses 4 bytes and is suitable when you need decimals but can tolerate some rounding errors.

double stores decimal numbers with double the precision of float (about 15-16 digits) and uses 8 bytes. It is best when you need more accurate decimal calculations, like in scientific computations.

⚖️

Code Comparison

c
#include <stdio.h>

int main() {
    int a = 5;
    float b = 5.1234567f;
    double c = 5.123456789012345;

    printf("int a = %d\n", a);
    printf("float b = %.7f\n", b);
    printf("double c = %.15f\n", c);

    return 0;
}
Output
int a = 5 float b = 5.1234565 double c = 5.123456789012345
↔️

Double Equivalent

c
#include <stdio.h>

int main() {
    double x = 3.141592653589793;
    printf("Double value: %.15f\n", x);
    return 0;
}
Output
Double value: 3.141592653589793
🎯

When to Use Which

Choose int when you need to store whole numbers like counts, indexes, or flags without decimals. Use float when you want to save memory and only need approximate decimal values, such as in graphics or simple calculations. Opt for double when precision matters, like in scientific calculations, financial data, or when rounding errors must be minimized.

Key Takeaways

Use int for exact whole numbers without decimals.
Use float for decimal numbers with moderate precision and less memory use.
Use double for high precision decimal numbers and scientific calculations.
Memory usage increases from int to float to double.
Precision and range are smallest in int, moderate in float, and largest in double.