ceil() Function
The ceil()
function in C rounds a given floating-point number upward, returning the smallest integral value that is not less than the input, represented as a floating-point number.
Syntax of ceil()
</>
Copy
double ceil(double x);
Parameters
Parameter | Description |
---|---|
x | Value to round up. |
Return Value
The function returns the smallest integral value not less than the input, expressed as a floating-point number.
It is important to note that ceil()
always rounds upward, which means even for negative numbers the function will return the nearest integer that is greater than or equal to the original value.
Examples for ceil()
Example 1: Rounding a Positive Floating-Point Number Upward
This example demonstrates how to round a positive floating-point number upward using ceil()
.
Program
</>
Copy
#include <stdio.h>
#include <math.h>
int main() {
double value = 3.2;
double result = ceil(value);
printf("ceil(%.1f) = %.1f\n", value, result);
return 0;
}
Explanation:
- The program initializes a floating-point number with the value
3.2
. - The
ceil()
function rounds this value upward, resulting in4.0
. - The rounded result is printed to the console.
Output:
ceil(3.2) = 4.0
Example 2: Rounding a Negative Floating-Point Number Upward
This example demonstrates how ceil()
rounds a negative floating-point number upward.
Program
</>
Copy
#include <stdio.h>
#include <math.h>
int main() {
double value = -2.7;
double result = ceil(value);
printf("ceil(%.1f) = %.1f\n", value, result);
return 0;
}
Explanation:
- The program initializes a floating-point number with the value
-2.7
. - The
ceil()
function rounds this value upward, yielding-2.0
, which is the smallest integer not less than-2.7
. - The result is then printed to the console.
Output:
ceil(-2.7) = -2.0