C floor() Function

Definition
In C, floor() rounds x down toward negative infinity. floor(2.9) is 2, and floor(-2.1) is -3.
You pass one double. The result is still a double, not an int. Include <math.h>.
Syntax
Pass the number you want to round down. Start with this call.
| Call | What it does |
|---|
floor(2.9) | Rounds down to 2. |
Include <math.h>. On GCC, link with -lm. This is not the same as cutting off the fraction. For that, use trunc().
Library declarations
double floor(double x);
float floorf(float x);
long double floorl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. The double to round toward -infinity. |
Return Value
| Return Value | Description |
|---|
| double | The largest integer value that is not greater than x, stored as a double. |
C Examples
Example 1 — Basic Floor
Round a positive non-integer down to the previous whole number.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", floor(5.78));
printf("%.1f\n", floor(5.0));
return 0;
}
How It Works
- Include math.h. That declares
floor. - 5.78 rounds down. The largest integer ≤ 5.78 is 5.
- 5.0 stays 5.0. Whole numbers are left unchanged.
- Print as double.
%.1f shows one decimal place.
Example 2 — Negative Numbers
Toward -infinity means negatives with a fraction become more negative—not closer to zero.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("floor(-2.3) = %.1f\n", floor(-2.3));
printf("ceil(-2.3) = %.1f\n", ceil(-2.3));
return 0;
}
floor(-2.3) = -3.0
ceil(-2.3) = -2.0
How It Works
- floor goes down the number line. From -2.3, the next integer toward -infinity is -3.
- ceil goes the other way.
ceil(-2.3) is -2. - Not “toward zero.” That job belongs to
trunc. - Compare both. Side-by-side output makes the directions clear.
Example 3 — How Many Full Batches Fit
Count complete groups when a partial group does not count.
#include <stdio.h>
#include <math.h>
int main(void) {
int items = 25;
int batch = 10;
int full = (int)floor(items / (double)batch);
printf("full batches = %d\n", full);
return 0;
}
How It Works
- Divide as double.
25 / (double)10 is 2.5. - Floor the quotient.
floor(2.5) is 2.0—only complete batches. - Cast for printing. Safe here because the count is small.
- Contrast with ceil. Pages needed would use
ceil; leftover ignored uses floor.
Frequently Asked Questions
floor() rounds x down toward negative infinity. floor(2.9) is 2, and floor(-2.1) is -3.
x is required. It is the double to round.
A double holding an integer value. It is not an int.
Include <math.h>. On GCC, link with -lm.
No. floor() goes toward negative infinity. trunc() goes toward zero, so trunc(-2.1) is -2.
Next: fmod()
Get the floating-point remainder after dividing two doubles.
fmod() tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful