C Math ceil() Function

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

What You’ll Learn

The ceil() function (short for ceiling) returns the smallest integer that is greater than or equal to a floating-point value. It always rounds toward positive infinity—the practical “round up” for positive numbers.

01

Ceiling

Round up +.

02

math.h

Link with -lm.

03

Returns double

Not int.

04

Negatives

Toward +∞.

05

vs floor

Opposite.

06

Pages

Count items.

Definition and Usage

Mathematically, ceil(x) is the smallest integer n such that n ≥ x. On the number line, you move right (toward +∞) until you hit an integer.

For positive fractions like 8.45, that feels like “round up” to 9. For negatives, remember: ceil(-2.3) is −2, not −3, because −2 is greater than −2.3.

💡
Beginner Tip

Need page count for 47 items with 10 per page? ceil(47.0 / 10.0) gives 5 pages—a classic ceil pattern.

📝 Syntax

Standard C declaration:

C
double ceil(double x);

Related variants

C
float ceilf(float x);           /* <math.h> */
long double ceill(long double x); /* <math.h> */

Parameters

  • x — any floating-point value.

Return Value

  • Smallest integer ≥ x, returned as double.
  • ceil(8.45) returns 9.0; ceil(8.0) returns 8.0.

Headers and linking

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

⚡ Quick Reference

CallMeaningResult
ceil(8.45)Positive fraction9.0
ceil(8.0)Already integer8.0
ceil(-2.3)Toward +∞-2.0
ceil(47.0/10)Page count5.0
floor(8.45)Opposite dir.8.0
Ceiling
ceil(x)

Toward +∞

Floor
floor(x)

Toward -∞

Nearest
round(x)

Closest int

To int
(int)ceil(x)

Cast carefully

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Rounding functions are common in UI layout and billing code.

📚 Getting Started

Round a positive decimal up to the next integer.

Example 1 — Round Up a Positive Decimal

Apply ceil to 8.45.

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

int main(void) {
    double number = 8.45;
    double rounded_up = ceil(number);

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

    return 0;
}

How It Works

8.45 lies between 8 and 9. ceil picks 9 because it is the smallest integer not less than 8.45.

Example 2 — Value Already an Integer

ceil leaves whole numbers unchanged.

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

int main(void) {
    double x = 5.0;

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

    return 0;
}

How It Works

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

📈 Practical Patterns

Negatives, pagination, and comparison with floor.

Example 3 — Ceiling of a Negative Number

ceil moves toward +∞, not toward a larger magnitude.

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

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

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

    return 0;
}

How It Works

This fixes the vague old note about negatives: ceil(-2.3) is −2, not −3. Use floor if you need the more-negative integer.

Example 4 — Calculate Number of Pages

How many pages for 47 items with 10 per page?

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

int main(void) {
    int total_items = 47;
    int per_page = 10;
    int pages = (int)ceil((double)total_items / per_page);

    printf("%d items, %d per page -> %d pages\n",
           total_items, per_page, pages);

    return 0;
}

How It Works

47 ÷ 10 = 4.7. You need 5 pages because the last page holds the remaining 7 items. ceil captures that partial page.

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

See how the three 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     ceil   floor  round\n");
    for (i = 0; i < 5; i++) {
        double x = vals[i];
        printf("%5.1f  %5.1f  %5.1f  %5.1f\n",
               x, ceil(x), floor(x), round(x));
    }

    return 0;
}

How It Works

Pick the function that matches your rounding direction: ceil (+∞), floor (−∞), or round (nearest).

🚀 Common Use Cases

  • Pagination — compute pages from item count and page size.
  • Billing — charge for full units when any fraction counts (e.g. storage blocks).
  • Graphics layout — allocate enough rows/columns for fractional tile counts.
  • Memory allocation — size buffers to fit fractional element counts.
  • Pair with floor() — bracket a value between floor and ceil.

🧠 How ceil() Works

1

Receive double x

Any finite floating-point input.

Input
2

Find integer ≥ x

Move right on the number line to the next integer.

Compute
3

Return as double

Integral value stored in floating-point format.

Output
=

Ceiling 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 “away from zero.”
  • ceil(-2.3) is −2; do not assume “more negative.”
  • Link with -lm when using GCC/Clang on Unix-like systems.
  • Compare with floor(), round(), and trunc() for other rounding needs.

⚡ Optimization

ceil() is implemented in the platform math library. For positive values where you only need integers, some code uses (int)x + (x > (int)x) tricks—but ceil handles negatives and edge cases correctly. Prefer the library function unless profiling shows a hot path that needs micro-optimization.

Conclusion

ceil() gives the smallest integer not less than x, rounding toward positive infinity. Use it for pagination, capacity planning, and anywhere a partial unit must count as a full one.

Continue with cos() for trigonometry, or explore floor() for the opposite rounding direction.

💡 Best Practices

✅ Do

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

❌ Don’t

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

Key Takeaways

Knowledge Unlocked

Five things to remember about ceil()

Ceiling rounding in C, explained simply.

5
Core concepts
📚 02

+∞ direction

Not abs.

Rule
📈 03

double out

Cast to int.

Types
📄 04

Pages

ceil(n/k).

Pattern
🔄 05

vs floor

Opposite.

Compare

❓ Frequently Asked Questions

ceil() returns the smallest integer value that is greater than or equal to x, as a double. For example, ceil(8.45) is 9.0 and ceil(8.0) is still 8.0.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double ceil(double x).
ceil() rounds toward positive infinity, not toward a larger absolute value. ceil(-2.3) is -2.0 (because -2 is greater than -2.3), not -3.
ceil(x) rounds toward +infinity (smallest integer >= x). floor(x) rounds toward -infinity (largest integer <= x). ceil(2.1)=3, floor(2.1)=2.
No. ceil() returns double. Cast to int only when the value fits in your integer type: int n = (int)ceil(x); watch overflow for huge x.
round() goes to the nearest integer (half away from zero). ceil() always moves toward +infinity. ceil(2.1) and round(2.1) are both 3, but ceil(2.5) is 3 while round(2.5) is 3 on typical implementations—ceil(-2.5) is -2, round(-2.5) is -3.
Did you know?

The name ceil comes from the mathematical “ceiling function” ⌊x⌋’s partner ⌈x⌉. In C, floor and ceil together let you bracket any real number between two consecutive integers.

Explore C Math Functions

Continue with cos() 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