C ceil() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
ceil(2.1);
CallWhat 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
C
double ceil(double x);
float ceilf(float x);
long double ceill(long double x);

Parameters

ParameterDescription
xRequired. The double to round toward +infinity.

Return Value

Return ValueDescription
doubleThe 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.

C
#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

  1. Include math.h. That declares ceil.
  2. 8.45 rounds up. The smallest integer ≥ 8.45 is 9.
  3. 8.0 stays 8.0. Whole numbers are left unchanged.
  4. 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.

C
#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;
}

How It Works

  1. ceil goes up the number line. From -2.3, the next integer toward +infinity is -2.
  2. floor goes the other way. floor(-2.3) is -3.
  3. Not “more negative.” Ceiling does not mean larger absolute value.
  4. 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.

C
#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

  1. Divide as double. 25 / (double)10 is 2.5, not integer truncating division.
  2. Ceil the quotient. ceil(2.5) is 3.0 so a partial page still counts.
  3. Cast for printing. (int) is safe here because page counts are small.
  4. 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.

Next: copysign()

Compute cosine for angles measured in radians.

copysign() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful