The ceil function in C computes the smallest integer value not less than x
. In another word, you can say that ceil function computes the smallest integer that is greater than or equal to x
. The x
is the argument that is passed in the ceil().
It is declared in math.h
and takes one argument in the form of double and returns the value of type double.
Example,
Input : 3.5 Output : 4 Input : -2.3 Output : -2 Input : 4.9 Output : 5
Syntax of ceil function in C:
//Syntax of ceil function in c double ceil(double x);
Parameters:
x
=> floating types (double)
Return value:
The ceil functions return ⌈x⌉, expressed as a floating-point number.
You can also use the ceilf() function to work specifically with float and ceill() to work with long double type. See the below syntax.
//Syntax of other ceil functions float ceilf(float x); long double ceill(long double x);
C program to understand the working of ceil function:
Below mentioned C example code shows the usage of ceil().
#include <stdio.h> #include <math.h> int main () { double val1, val2, val3, val4; val1 = 3.5; val2 = -2.3; val3 = 3.8; val4 = 4.9; printf ("value1 = %.1lf\n", ceil(val1)); printf ("value2 = %.1lf\n", ceil(val2)); printf ("value3 = %.1lf\n", ceil(val3)); printf ("value4 = %.1lf\n", ceil(val4)); return(0); }
Output:
value1 = 4.0
value2 = -2.0
value3 = 4.0
value4 = 5.0
Error handling
Errors are reported as specified in math_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
- The current rounding mode has no effect.
- If arg is ±∞, it is returned, unmodified.
- If arg is ±0, it is returned, unmodified.
- If arg is NaN, NaN is returned.
#include <math.h> #include <stdio.h> int main() { printf("ceil(-0.0) = %+.1f\n", ceil(-0.0)); printf("ceil(-Inf) = %+f\n", ceil(-INFINITY)); return 0; }
Output:
ceil(-0.0) = -0.0
ceil(-Inf) = -inf
Recommended Post:
- Use of pow function in C language.
- C program to calculate the power of a number.
- sqrt function in C.
- C program to find all roots of a quadratic equation using switch case.
- C program to find the roots of a quadratic equation.
- How to find whether a given number is prime number in C?
- Use of isxdigit in C programming.
- How to use ispunct function in C programming?
- tolower function in C.
- How to use the islower function in C?
- Use of iscntrl function in C.