Horje
C ceil() Function

In the C language, the <math.h> header file contains the Standard Math Library that provides various mathematical functions, including the ceil() function. In this article, we will see how to use the ceil() function in C.

What is ceil() in C?

C ceil() is a built-in library function that computes the smallest integer value greater than or equal to the given floating-point number. It is defined inside the <math.h> header file with its prototype as follows:

Syntax of ceil()

double ceil(double x);

Parameters

  • x: This is the floating-point number you want to round up. It can be of type float, double, or long double.

Return Value

The ceil() function returns the smallest integer value greater than or equal to the input number as a double.

Example of ceil() in C

Input:
double number = 2.9;

Output:
The floor value of 2.9 is 3

The given below program demonstrates how we can calculate the ceil value of various numbers in C.

C
// C Program to calculate the ceiling value of various
// numbers in C

#include <math.h>
#include <stdio.h>

int main()
{
    double num1 = 1.4;
    double num2 = 1.5;
    double num3 = -1.5;
    double num4 = -1.6;

    // printing the numbers
    printf("Number 1: %.1f\n", num1);
    printf("Number 2: %.1f\n", num2);
    printf("Number 3: %.1f\n", num3);
    printf("Number 4: %.1f\n", num4);

    // printing the return value of the ceil() function
    printf("Ceiling value of num1: %.1f\n", ceil(num1));
    printf("Ceiling value of num2: %.1f\n", ceil(num2));
    printf("Ceiling value of num3: %.1f\n", ceil(num3));
    printf("Ceiling value of num4: %.1f\n", ceil(num4));

    return 0;
}

Output
Number 1: 1.4
Number 2: 1.5
Number 3: -1.5
Number 4: -1.6
Ceiling value of num1: 2.0
Ceiling value of num2: 2.0
Ceiling value of num3: -1.0
Ceiling value of num4: -1.0

How ceil() in C works?

The C ceil() function works by rounding a floating-point number up to the smallest integer value that is greater than or equal to the given number. Here’s a breakdown:

  • For positive numbers, it rounds up to the next integer.
  • For negative numbers, it rounds towards zero to the next integer.

Conclusion

In this article, we discussed the C standard library function ceil() which is used to round floating-point numbers up to the nearest integer. The standard library contains useful and frequently used functions that make programming easier by avoiding the need to rewrite common functions repeatedly.

Frequently Asked Questions on C ceil() Function

What does the ceil() function return for a positive floating-point number?

The ceil() function returns the smallest integer value greater than or equal to the positive floating-point number.

Can the ceil() function handle negative numbers?

Yes, the ceil() function can handle negative numbers and rounds them towards zero to the next integer.

How does ceil() differ from other rounding functions like floor() and round()?

The ceil() function always rounds up to the nearest integer, floor() always rounds down to the nearest integer, and round() rounds to the nearest integer based on the fractional part (0.5 or greater rounds away from zero, less than 0.5 rounds towards zero).




Reffered: https://www.geeksforgeeks.org


C Language

Related
Line Intersection in C Line Intersection in C
How to Read Input Until EOF in C? How to Read Input Until EOF in C?
C Program to Implement Adjacency List C Program to Implement Adjacency List
How to Use the Volatile Keyword in C? How to Use the Volatile Keyword in C?
How to Split a String by Multiple Delimiters in C? How to Split a String by Multiple Delimiters in C?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19