C Decimal Precision

C Decimal Precision (Beginner → Advanced)
Decimal precision in C language means how many digits are stored and displayed after the decimal point.
This depends on:
the data type (
float,double,long double)how you print the value (
printfformat)how floating-point numbers are stored internally (IEEE-754)
Decimal Precision by Data Type
| Data Type | Size (typical) | Precision (approx) |
|---|---|---|
float | 4 bytes | ~6–7 decimal digits |
double | 8 bytes | ~15–16 decimal digits |
long double | 10–16 bytes | ~18+ digits (platform dependent) |
float Precision Example
Output (approx):
- Extra digits are lost because
floatcan’t store them accurately.
double Precision Example
Output:
doublepreserves more digits.
Controlling Decimal Places in printf() (Most Important)
Syntax
n = number of digits after decimal
Example
- Rounding happens automatically
Decimal Precision with float vs double
Output (approx):
- Shows precision difference clearly
Why Decimal Precision Is Inexact?
C uses binary floating-point (IEEE-754).
Some decimal numbers cannot be represented exactly in binary.
Famous Example
Output:
- This is normal behavior, not a bug.
Comparing Floating-Point Numbers
Wrong Way
Correct Way (Using Tolerance)
- Always compare using a small epsilon
Decimal Precision in Calculations
- Integer division happens before assignment
- Type casting fixes precision
Using long double for High Precision
- Best for scientific & financial calculations
float, double, long double – When to Use?
| Use Case | Recommended Type |
|---|---|
| Memory-critical (embedded) | float |
| General calculations | double |
| High-precision math | long double |
| Money (exact values) | Floating types (use integers) |
Common Mistakes
- Expecting exact decimal values
- Comparing floats with
== Usingfloatfor money- Forgetting format precision in
printf()
Interview Questions (Very Important)
Difference between
floatanddoubleprecision?Why
0.1 + 0.2 != 0.3?How to print only 2 decimal places?
How to safely compare floating numbers?
When to use
long double?
Summary
- Decimal precision depends on data type
float≈ 6 digits,double≈ 15 digits- Use
%.nfto control output precision - Floating numbers are approximate
- Never compare floats using
== Critical for accuracy, interviews & real programs
