C Math floor() Function

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The floor() function returns the largest integer that is less than or equal to a floating-point value. It always rounds toward negative infinity—the practical “round down” for positive numbers.

01

Floor

Round down −.

02

math.h

Link with -lm.

03

Returns double

Not int.

04

Negatives

Toward −∞.

05

vs ceil

Opposite.

06

vs trunc

On negatives.

Definition and Usage

Mathematically, floor(x) is the largest integer n such that n ≤ x. On the number line, you move left (toward −∞) until you hit an integer.

For positive fractions like 5.78, that feels like “round down” to 5. For negatives, remember: floor(-2.3) is −3, not −2, because −3 is less than −2.3.

💡
Beginner Tip

Need only complete batches of 10 from 47 items? floor(47.0 / 10.0) gives 4 full batches—use ceil instead if partial batches still count as a page.

📝 Syntax

Standard C declaration:

C
double floor(double x);

Related variants

C
float floorf(float x);           /* <math.h> */
long double floorl(long double x); /* <math.h> */

Parameters

  • x — any floating-point value.

Return Value

  • Largest integer ≤ x, returned as double.
  • floor(5.78) returns 5.0; floor(5.0) returns 5.0.

Headers and linking

  • #include <math.h>
  • Compile: gcc floor.c -std=c11 -o floor -lm

⚡ Quick Reference

CallMeaningResult
floor(5.78)Positive fraction5.0
floor(5.0)Already integer5.0
floor(-2.3)Toward −∞-3.0
floor(47.0/10)Full batches4.0
ceil(5.78)Opposite dir.6.0
Floor
floor(x)

Toward −∞

Ceiling
ceil(x)

Toward +∞

Truncate
trunc(x)

Toward zero

To int
(int)floor(x)

Cast carefully

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Rounding functions are common in UI layout, pricing, and game grids.

📚 Getting Started

Round a positive decimal down to the nearest integer.

Example 1 — Round Down a Positive Decimal

Apply floor to 5.78.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double number = 5.78;
    double rounded_down = floor(number);

    printf("Original number: %.2f\n", number);
    printf("Rounded down:    %.2f\n", rounded_down);

    return 0;
}

How It Works

5.78 lies between 5 and 6. floor picks 5 because it is the largest integer not greater than 5.78.

Example 2 — Value Already an Integer

floor leaves whole numbers unchanged.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 5.0;

    printf("floor(%.1f) = %.1f\n", x, floor(x));

    return 0;
}

How It Works

When x is already an integer, floor(x) == x. No change occurs.

📈 Practical Patterns

Negatives, complete batches, and comparison with ceil.

Example 3 — Floor of a Negative Number

floor moves toward −∞, not toward zero.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = -2.3;

    printf("x = %.1f\n", x);
    printf("floor(x) = %.1f  (toward -infinity)\n", floor(x));
    printf("ceil(x)  = %.1f  (toward +infinity)\n", ceil(x));
    printf("trunc(x) = %.1f  (toward zero)\n", trunc(x));

    return 0;
}

How It Works

floor(-2.3) is −3, not −2. This is the most common beginner mistake—do not confuse floor with “chop off the decimal part.”

Example 4 — Count Complete Full Batches

How many complete groups of 10 fit in 47 items?

C
#include <stdio.h>
#include <math.h>

int main(void) {
    int total_items = 47;
    int per_batch = 10;
    int full_batches = (int)floor((double)total_items / per_batch);
    int remainder = total_items % per_batch;

    printf("%d items, %d per batch\n", total_items, per_batch);
    printf("Full batches: %d\n", full_batches);
    printf("Left over:    %d items\n", remainder);

    return 0;
}

How It Works

47 ÷ 10 = 4.7. Only 4 full batches exist; 7 items remain. Use ceil if a partial batch still needs its own slot.

Example 5 — floor() vs ceil() vs round() vs trunc()

See how four rounding functions differ on the same values.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double vals[] = { 2.3, 2.5, 2.8, -2.3, -2.5 };
    size_t i;

    printf("   x     floor  ceil  round  trunc\n");
    for (i = 0; i < 5; i++) {
        double x = vals[i];
        printf("%5.1f  %5.1f  %5.1f  %5.1f  %5.1f\n",
               x, floor(x), ceil(x), round(x), trunc(x));
    }

    return 0;
}

How It Works

On negatives, floor and trunc diverge. Choose the direction that matches your business rule.

🚀 Common Use Cases

  • Conservative billing — charge only for complete units.
  • Grid indexing — map pixel position to tile row/column.
  • Inventory — count how many full crates fit in stock.
  • Time buckets — complete hours/minutes elapsed.
  • Pair with ceil() — bracket a value: floor(x) ≤ x ≤ ceil(x).

🧠 How floor() Works

1

Receive double x

Any finite floating-point input.

Input
2

Find integer ≤ x

Move left on the number line to the previous integer.

Compute
3

Return as double

Integral value stored in floating-point format.

Output
=

Floor value

Cast to int when you need a whole number for indexing.

📝 Notes

  • Returns double, not int—cast explicitly when needed.
  • Rounds toward −infinity, not always “toward zero.”
  • floor(-2.3) is −3; do not assume “drop decimals.”
  • Link with -lm when using GCC/Clang on Unix-like systems.
  • Compare with ceil(), round(), and trunc() for other rounding needs.

⚡ Optimization

floor() is implemented in the platform math library. For positive values only, some code uses (int)x when truncation toward zero equals floor—but that breaks for negatives. Prefer floor unless profiling shows a hot path that needs micro-optimization.

Conclusion

floor() gives the largest integer not greater than x, rounding toward negative infinity. Use it for complete batches, conservative rounding, and grid math—but pick ceil when partial units still count.

Continue with fmod() for remainders, or explore ceil() for the opposite rounding direction.

💡 Best Practices

✅ Do

  • Include <math.h> and link with -lm
  • Use floor for “how many complete groups”
  • Cast to int after checking range
  • Compare with ceil and trunc for clarity
  • Remember negatives round toward −∞

❌ Don’t

  • Assume floor means “toward zero”
  • Forget the return type is double
  • Use floor when ceil is what you need
  • Cast huge doubles to int without overflow checks
  • Mix integer division before floor (use doubles)

Key Takeaways

Knowledge Unlocked

Five things to remember about floor()

Floor rounding in C, explained simply.

5
Core concepts
📚 02

−∞ dir.

Not zero.

Rule
📈 03

double out

Cast to int.

Types
📄 04

Batches

floor(n/k).

Pattern
🔄 05

vs ceil

Opposite.

Compare

❓ Frequently Asked Questions

floor() returns the largest integer value that is less than or equal to x, as a double. For example, floor(5.78) is 5.0 and floor(5.0) is still 5.0.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double floor(double x).
floor() rounds toward negative infinity, not toward zero. floor(-2.3) is -3.0 (because -3 is less than -2.3), not -2.
floor(x) rounds toward -infinity (largest integer <= x). ceil(x) rounds toward +infinity (smallest integer >= x). floor(2.1)=2, ceil(2.1)=3.
For positive numbers they match. For negatives, floor(-2.3) is -3 (toward -infinity) while trunc(-2.3) is -2 (toward zero). Pick based on rounding direction.
No. floor() returns double. Cast to int only when the value fits: int n = (int)floor(x); check range before casting huge values.
Did you know?

For any real x, floor(x) and ceil(x) are consecutive integers that bracket x: floor(x) ≤ x ≤ ceil(x). Together they define the integer part above and below any decimal.

Explore C Math Functions

Continue with fmod() or browse the full math function reference.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful