Course contentsShow
← C
Lesson 103 of 1,2192. Variables and TypesFree lesson

The `<float.h>` header file

The <float.h> header file

What you'll learn: How to use the standard library to discover the exact physical limits and precision of floating-point numbers on your specific computer.

The Instruction Manual for Decimals

Imagine you buy a set of measuring cups. To use them correctly, you need to know two things: what is the smallest drop they can measure, and what is the largest volume they can hold before overflowing? In C, the <float.h> header file is the "instruction manual" for your decimal types (float, double, and long double).

While a float generally follows standard rules, different hardware (like a tiny smartwatch versus a powerful server) might handle the math slightly differently. Instead of guessing how much precision you have, you can ask <float.h> for the facts.

Checking the Limits

This header file provides a list of "constants"—predefined names that hold specific values about your system. The most important ones tell you the maximum value, the minimum positive value, and the "precision" (how many decimal digits are guaranteed to be accurate).

Here is how you can peer inside the limits of your machine:

#include <stdio.h>
#include <float.h>

int main() {
    // FLT_DIG: The number of reliable decimal digits in a float
    printf("Float precision: %d digits\n", FLT_DIG);

    // FLT_MAX: The largest number a float can hold
    printf("Max float value: %e\n", FLT_MAX);

    // DBL_MAX: The largest number a double can hold
    printf("Max double value: %e\n", DBL_MAX);

    // FLT_EPSILON: The smallest difference between 1.0 and the next float
    printf("Float epsilon: %e\n", FLT_EPSILON);

    return 0;
}

Why Epsilon Matters?

One unique constant in this file is FLT_EPSILON. Think of this as the "graininess" of your numbers. Floating-point numbers aren't a continuous smooth line; they are made of tiny steps. Epsilon tells you the size of the smallest possible step your computer can recognize at a specific scale. If you try to add a number smaller than the epsilon to a large total, the computer might literally ignore it because it falls "between the cracks" of its hardware limits.

By including <float.h>, you ensure your code is portable. Instead of hard-coding a limit like 3.4028e+38, you use FLT_MAX, and your program will automatically adapt if it is ever moved to a different type of processor.

Key takeaway

The <float.h> header provides predefined constants that reveal the maximum values and decimal precision of your computer's floating-point hardware.