The C ceil function is one of the math functions that returns the smallest integer value, greater than or equal to a given floating-point number or a specified expression. For example, ceil(10.20) returns 11.
C ceil() function syntax
The syntax of the math ceil function is as shown below.
double ceil(double number);
TIP: As you can see, it is a double data type. However, when you pass other data types, they are implicitly converted to double.
Parameters
It accepts a single parameter of double, float, and long data types. It means we can pass a floating-point number to the C ceil() function to round the value upwards.
Return Value
It returns the smallest floating-point number greater than or equal to the given parameter value. It rounds to the nearest integer but returns as a double value.
Required Header File
To use the ceil() function inside a program, we must include the <math.h> header file.
#include<math.h>
Apart from this, there are dedicated functions to work with the float and long data types.
- float ceilf(float x): Use for float data type.
- long double ceill(long double x): Use for float data type.
C ceil function with positive and negative numbers
This math.ceil() function allows you to find the smallest integer value greater than or equal to a given number. In this program, we will find the ceiling value of positive and negative floating-point numbers and display the output.
- For positive numbers: The ceil() function rounds to the next integer. It moves towards positive infinity.
- For negative numbers: The ceil() function rounds or moves towards zero.
#include <stdio.h>
#include <math.h>
int main()
{
printf("\n The Ceil Value of 0.75 = %.2f ", ceil(0.75));
printf("\n The Ceil Value of 12.25 = %.2f ", ceil(12.25));
printf("\n The Ceil Value of 152.50 = %.2f ", ceil(152.50));
printf("\n The Ceil Value of -12.36 = %.2f ", ceil(-12.36));
printf("\n The Ceil Value of -27.82 = %.2f ", ceil(-27.32));
printf("\n The Ceil Value of -90.50 = %.2f ", ceil(-90.50));
return 0;
}
Let me use one positive and one negative value in the above screenshot to explain the ceil() function’s functionality.
- 0.75: The next immediate integer that is greater than 0.75 is 1 (1 > 0.75).
- -12.36: The smallest number greater than -12.36 is -12 (-12 > -12.36). For negative, it moves towards zero.
As the ceil() function returns the output as the double data type, it returns 1.00 and -12.00 as the result.

math ceil on user-entered float-point number
In this C Programming example, we allow the users to enter their own values. Next, the program uses the math ceil function to find the closest numbers.
#include <stdio.h>
#include <math.h>
int main()
{
float number, ceilValue;
printf(" Please enter any Numeric: ");
scanf("%f", &number);
ceilValue = ceil(number);
printf("\n The Ceiling of %.2f = %.4f ", number, ceilValue);
return 0;
}
Please enter any Numeric: 256.09
The Ceiling of 256.09 = 257.0000
Difference between C ceil()and floor()
Both ceil() and floor() functions are used to round the floating-point numbers.
- ceil(): It always moves toward positive infinity.
- floor(): It always moves towards negative infinity.
From the example below,
- ceil(50.75): The immediate next integer value towards positive infinity is 51.
- floor(50.75): The immediate integer towards negative infinity is 50.
#include <stdio.h>
#include <math.h>
int main()
{
double a = 50.75;
double b = -100.75;
printf("Positive ceil: %.2f\n", ceil(a));
printf("Positive floor: %.2f\n", floor(a));
printf("Negative ceil: %.2f\n", ceil(b));
printf("Negative floor: %.2f\n", floor(b));
return 0;
}
Positive ceil: 51.00
Positive floor: 50.00
Negative ceil: -100.00
Negative floor: -101.00
ceil() vs ceilf() vs ceill()
There are three variants of the ceil() function in C programming language.
- ceil(): It works with floating-point values. Use a double data type.
- ceilf(): Use this method for the float data type because it returns the float as the output.
- ceill(): Use it for the long double data types.
#include <stdio.h>
#include <math.h>
int main()
{
double d = 150.60;
float f = 20.14f;
long double ld = 99.55L;
printf("%.2f\n", ceil(d));
printf("%.2f\n", ceilf(f));
printf("%.2Lf\n", ceill(ld));
return 0;
}
151.00
21.00
100.00
C ceil() function vs Type Casting
For negative numbers, both the ceil() function and the type casting approach return the same value. However, for a positive number, type casting truncates the fractional values and moves towards zero. On the other hand, the ceil() function moves towards positive infinity and returns the closest integer.
#include <stdio.h>
#include <math.h>
int main()
{
double a = 10.5;
double b = -10.75;
printf("Positive ceil: %.2f\n", ceil(a));
printf("Type Casting: %d\n", (int)a);
printf("Negative ceil: %.2f\n", ceil(b));
printf("Type Casting: %d\n", (int)b);
return 0;
}
Positive ceil: 11.00
Type Casting: 10
Negative ceil: -10.00
Type Casting: -10
FAQs
Convert ceil() result to int: Memory allocation example
As we all know, each memory block is of 4096 bytes, and the requested memory is 15000 bytes.
#include <stdio.h>
#include <math.h>
int main()
{
int requested = 15000;
int sizeOfaBlock = 4096;
int blocksNeeded = (int)ceil((double)requested / sizeOfaBlock);
printf("Required Blocks: %d\n", blocksNeeded);
return 0;
}
Required Blocks: 4
C ceil() function with zero, integer, infinity, and nan values
- Integer: When you pass an integer input, the ceil() function returns the same value without any changes. For instance, ceil(10.0) = 10.0.
- Zero: For both positive and negative zeros, it returns the same output. Ceil(o.0) = 0 and ceil(-0.0) = -0.0.
- Infinity: It returns inf as the output. For instance, ceil(INFINITY) = inf.
- NaN: It returns nan as the output. For example, ceil(NAN) = nan.