Hello Coders! In this post, we will discuss Ceil Function in C. Before we move forward let us first understand what is a ceil function.

In C Programming Language, the **ceil function** returns the least integer that is larger than or equal to *x* (i.e: rounds up the most approaching integer).

Table of Contents

## Ceil Function

We know that the ceil function returns the least integer which is larger than or equal to x. Now, let us understand the ceil function.

** Syntax:** The syntax for the ceil function in the C Language is double ceil (double x);

** Parameters or Arguments**: (x) The value will basically round up to the nearest integer.

** Returns:** The ceil function returns the smallest integer that is greater than or equal to

*x*.

** Required Header**: In the C Language, the required header for the ceil function is #include <math.h>

## Points to Remember

The CEIL and CEILING functions round the specified number up, and return the smallest number that is greater than or equal to the specified number.The specified number must be a double precision number.

- If the specified number is NULL, the result of these functions is NULL.
- Moreover, if the number is equal to a mathematical integer, the result of these functions is the same as the specified number.
- Thirdly, if the number is zero (0), the result of these functions is zero.
- Lastly, a number less than zero but greater than -1.0, then the result is zero.

The returned value is the smallest (closest to negative infinity) double floating-point value that is greater than or equal to the specified number. The returned value is equal to a mathematical integer. The data type of the returned value is a DOUBLE PRECISION number

**Difference between round and ceil function**–

- round( ) function in C returns the nearest integer value of the float/double/long double argument passed to this function.
- If the decimal value is from ”.1 to .5″, it returns an integer value less than the argument. If the decimal value is from “.6 to .9″, it returns the integer value greater than the argument.

Example of round function:

```
#include <stdio.h>
#include <math.h>
int main()
{
float i=5.4, j=5.6;
printf("round of %f is %f\n", i, round(i));
printf("round of %f is %f\n", j, round(j));
return 0;
}
```

```
round of 5.400000 is 5.000000
round of 5.600000 is 6.000000
```

Here if we had used the ceil function then output in both the cases would be 6 i.e ceil(5.4) is 6 and ceil(5.6) is also 6.

## Example #1

This example illustrates how is ceil function normally executed.

```
#include <stdio.h>
#include <math.h>
int main()
{
double num = 8.33;
int result;
result = ceil(num);
printf("Ceiling integer of %.2f = %d", num, result);
return 0;
}
```

`Ceiling integer of 8.33 = 9`

As we see in the above code, 8.33 is rounded off to its nearest integer value i.e 9 with the help of the ceil function.

## Example #2

This example will help you understand the ceil function in a better way.

```
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{ //Define temporary variables
double a=12.1,b=12.9,c=-12.1,d=-12.9;
//Calculate and display ceil value for given value
printf("ceil value of a: %f\n",ceil(a));
printf("ceil value of b: %f\n",ceil(b));
printf("ceil value of c: %f\n",ceil(c));
printf("ceil value of d: %f\n",ceil(d));
return 0;
}
```

*Output:*

```
ceil value of a: 13.000000
ceil value of a: 13.000000
ceil value of a: -12.000000
ceil value of a: -12.000000
```

As you can see in the above example, the positive values i.e 12.1 and 12.9 are rounded off to 13 whereas -12.1 and -12.9 are considered to be 12.

## Example #3

In this example we will take input from the user.

```
#include <stdio.h>
#include <math.h>
int main()
{
float val;
float cVal;
printf("Enter a float value: ");
scanf("%f",&val);
cVal =ceil(val);
printf("ceil value:%f\n",cVal);
return 0;
}
```

** Output**:

```
Enter a float value: 123.45
ceil value:124.000000
```

## Other inbuilt arithmetic functions in C

“math.h” and “stdlib.h” header files support all the arithmetic functions in the C language. All the arithmetic functions used in the C language are given below.

- abs ( ) : This function returns the absolute value of an integer. The absolute value of a number is always positive. Only integer values are supported in C.
- floor ( ): This function returns the nearest integer which is less than or equal to the argument passed to this function.
- round ( ): This function returns the nearest integer value of the float/double/long double argument passed to this function. If a decimal value is from “.1 to .5”, it returns an integer value less than the argument. If the decimal value is from “.6 to .9”, it returns the integer value greater than the argument.
- sin ( ): We use this function to calculate sine value.
- cos ( ): We use this function to calculate cosine.
- cosh ( ): We use this function to calculate hyperbolic cosine.
- exp ( ): We use this function to calculate the exponential “e” to the xth power.
- tan ( ): We use this function to calculate tangent.
- tanh ( ): We use this function to calculate hyperbolic tangent.
- sinh ( ): We use this function to calculate hyperbolic sine.
- log ( ): We use this function to calculates the natural logarithm.
- log10 ( ): We use this function to calculates the base 10 logarithms.
- sqrt ( ): We use this function to find the square root of the argument passed to this function.
- pow ( ): We use this function to find the power of the given number.
- trunc.(.): This function truncates the decimal value from the floating-point value and returns the integer value.

## Homework

- Can you find the output for the below code?

```
#include <stdio.h>
#include <math.h>
int main()
{
float number, ceilValue;
number=34.679
ceilValue = ceil(number);
printf("\n The Ceiling Value of %.2f = %.4f ", number, ceilValue);
return 0;
}
```

2. Can you find the output for the below code?

```
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{ //Define temporary variables
double a=14.5,b=14.9,c=-14.1,d=-14.7;
//Calculate and display ceil value for given value
printf("ceil value of a: %f\n",ceil(a));
printf("ceil value of b: %f\n",ceil(b));
printf("ceil value of c: %f\n",ceil(c));
printf("ceil value of d: %f\n",ceil(d));
return 0;
}
```

Do let us know your answers in the comment section below.

## Conclusion

Hope you find this post helpful. We have discussed the **Ceil Function in C **and how to implement it. We hope that you found it easy to understand.

Stay tuned for more posts and updates. Until then, Happy Coding!