C ceil() Function

Definition
In C, ceil() rounds x up toward positive infinity. ceil(2.1) is 3, and ceil(-2.1) is -2, which is the next integer toward +infinity.
You pass one double. The result is still a double, such as 3.0, not an int. Include <math.h>.
Syntax
Pass the number you want to round up. Start with this call.
| Call | What it does |
|---|
ceil(2.1) | Rounds up to 3. |
Include <math.h>. On GCC, link with -lm. The result type is still double.
Library declarations
double ceil(double x);
float ceilf(float x);
long double ceill(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. The double to round toward +infinity. |
Return Value
| Return Value | Description |
|---|
| double | The smallest integer value that is not less than x, stored as a double. |
C Examples
Example 1 — Basic Ceiling
Round a positive non-integer up to the next whole number.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", ceil(8.45));
printf("%.1f\n", ceil(8.0));
return 0;
}
How It Works
- Include math.h. That declares
ceil. - 8.45 rounds up. The smallest integer ≥ 8.45 is 9.
- 8.0 stays 8.0. Whole numbers are left unchanged.
- Print as double.
%.1f shows one decimal place.
Example 2 — Negative Numbers
Toward +infinity means negatives move closer to zero when they have a fractional part.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("ceil(-2.3) = %.1f\n", ceil(-2.3));
printf("floor(-2.3) = %.1f\n", floor(-2.3));
return 0;
}
ceil(-2.3) = -2.0
floor(-2.3) = -3.0
How It Works
- ceil goes up the number line. From -2.3, the next integer toward +infinity is -2.
- floor goes the other way.
floor(-2.3) is -3. - Not “more negative.” Ceiling does not mean larger absolute value.
- Compare both. Printing them side by side makes the directions clear.
Example 3 — Pages Needed (Ceiling Division)
Convert an item count into how many full pages you need when each page holds a fixed size.
#include <stdio.h>
#include <math.h>
int main(void) {
int items = 25;
int per_page = 10;
int pages = (int)ceil(items / (double)per_page);
printf("pages = %d\n", pages);
return 0;
}
How It Works
- Divide as double.
25 / (double)10 is 2.5, not integer truncating division. - Ceil the quotient.
ceil(2.5) is 3.0 so a partial page still counts. - Cast for printing.
(int) is safe here because page counts are small. - Real-world pattern. Pagination, packing, and batch sizes often need ceiling division.
Frequently Asked Questions
ceil() rounds x up toward positive infinity. ceil(2.1) is 3, and ceil(-2.1) is -2.
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. ceil moves toward positive infinity, so ceil(-2.1) is -2.
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