ceil() in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The ceil() function in C, declared in the math.h header file, is used to find the nearest integral value, which is not less than the given number(provided as a parameter to the function).

Example- Let's think of a situation when you want to calculate the upper bound integer for any number. In this case, ceil function could be helpful. Like if Number = 45.67, then ceil function will return 46, which is an upper bound integral value.

Syntax of ceil() in C

The syntax of the ceil() function in C is as below:

Function Prototype:

Parameters of ceil() in C

The ceil function in C accepts a single parameter of double as a data type.

Suppose the program provides some other data type, i.e. int, float, long long int etc.. In that case, the internal type casting will occur according to the specified prototype of the function, which means the argument will be typecast to double type.

In case of any provided data type, i.e., string, which could not implicitly typecast to double, the compile-time error will be raised.

Return Value of ceil() in C

Return Type: double

The ceil function in C returns an integral value of the double data type.

Ex- 22.000000, 142.000000, etc.

From the beginner's point of view, there may arise a question like if the ceil function is returning an integral value, then why the return type of function prototype is double instead of the int? Because, as we know, generally, the double data type is used to store the floating-point values(which contains some fractional part after the decimal point, i.e., 24.67853), then why we are using it for integral values, i.e. 89, 36, etc.

So the reason is that the range of double is much more than the int. Also, its range is greater than the unsigned long long int. That’s why this return type is used to avoid overflow.

We can use the int to store the return value of the ceil without any data loss, but it will work only till the return value is less enough to be stored in the int data type.

Example of ceil() in C

Here is a program to illustrate what we have discussed till now.

As we can see, this function will return the nearest integral value greater than 3.56, which is 4.

ceil example

Output:

What is ceil() in C?

The math library contains various useful functions that operate on some mathematical operands and perform simple and complex calculations. The ceil function is also one of those useful functions. It takes a single parameter and returns the nearest greater or equal integral value to the provided parameter.

what is ceil

There are two more prototypes related to the ceil() function based on the condition if the type of provided parameter is other than double.

1. If the parameter type is long double, we can use the ceill function.

2. If the parameter type is a float, we can use the ceilf function.

How To Use Ceil Function in C?

You can easily utilize the ceil function. Just follow these three steps,

  • Identify the data type or range of the parameter to be passed.
  • Choose the particular function among ceil, ceill, or ceilf according to the data type of parameter
  • Save the result in a variable whose data type is big enough to store the return value of the ceil() function in C.

More Examples

Example 1: Ceil Function with The Provided Argument as An Integer.

If we provide an integer to the ceil function in C, it will return the same integer.

Output:-

Example 2: Program To Ceil The Floating Number Using The ceil() Function in C

Output:-

Example 3: Program To Round Off The Floating Number Using The ceil() Function in C

The round off of any number means to make the number equal to the closest integral value; that could be greater than the number, smaller than the number, or equal to the number if it is an integer.

In the round-off technique, we find whether the fractional value written after the decimal point is greater than 0.5.

  • If YES, this number is closer to the next integral value. Ex- 65.78 is close to 66,
  • Otherwise, it is closer to the same integral value. Ex- 65.34 is close to 65 itself.

So we can round off the number with ceil function by subtracting 0.5 from the number. Let's deduce why we are doing this,

  • If the fractional value written after the decimal point is greater than 0.5 then if we subtract 0.5 from it, it will still contain some fractional part. Ex- 65.780.5=65.2865.78 - 0.5 = 65.28, means ceil function for 65.28 will return 66 which is close to 65.78.
  • Else If, The fractional value written after the decimal point is less than the 0.5 and subtraction of 0.5 from it will make its integral value 1 less than the original. Ex- 65.340.5=64.8465.34 - 0.5 = 64.84, now ceil function will return 65 which is close to `65.34.
  • Else, one more condition could be, when the fractional value after the decimal point is equal to 0.5, then subtraction of 0.5, will make it an integral value. Ex- 65.50.5=65.065.5 - 0.5 = 65.0, although in this case, 66 and 65 are close to 65.5 according to our technique, the ceil function will return exactly that integral value.

ceil round off

Output:

Example 4: Program To Round Off The Positive And Negative Numbers Using The ceil() Function in C

Output:

Conclusion

  • The ceil function in C returns the nearest integer greater than the provided argument.
  • If the integer is provided to the ceil function, it will return the same integer.
  • The double as return type is used to avoid overflow.
  • There are two more prototypes related to ceil function in C: ' ceillandceilf`. These are only different in the aspect of data types of the provided argument and the data type of return value.