Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax
3.
Examples
3.1.
Ceil a number using the ceil function
3.2.
Ceil a floating number using ceil function
4.
Characteristics of Ceil function
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Ceil Function in C

Introduction

The C programming language is a very powerful programming language. It contains a large number of predefined functions which are used for various purposes like Arithmetic, logical, Sorting, etc. These predefined functions are available in different header files. There is a predefined function in the C programming language in math.h header file called Ceil. The ceil function returns the nearest integer number, which is greater than or equal to the number passed as an argument. 

Syntax

The Ceil function takes a single value as its argument (a in our case). The function will return the smallest possible integer value to round up the passed number a. The common syntax used for ceil function in the C programming language is
 

Double ceil (double a)

You can also read about dynamic array in c,C Static Function

Examples

Ceil a number using the ceil function

Code

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

int main ()  
{  
    double a = 14.70;  
    int ans;  
    // round up to nearest value
    ans = ceil (a);  
      
    printf ( " \n The ceiling integer of given %.2f is %d ", a, ans);
    return 0;     
}  


Output

The ceiling integer of given 14.70 is 15 


Ceil a floating number using ceil function

Example

#include <stdio.h>  
#include <math.h>  
int main ()  
{  
    float x1=12.33, x2=-41.67;  
    float ans1=ceil(x1), ans2=ceil(x2);
    printf ("The ceiling value of positive floating number %.2f is %.2f \n", x1,ans1);  
    printf ("The ceiling value of negative floating number %.2f is %.2f \n ", x2, ans2);   
    return 0;  
}   


Output

The ceiling value of positive floating number 12.33 is 13.00 
The ceiling value of negative floating number -41.67 is -41.00 

You can practice by yourself with the help of online c compiler for good practice.

Must Read Passing Arrays to Function in C

Characteristics of Ceil function

  • It is a predefined/built-in function available in math.h header file.
  • The function returns the smallest number that is just greater than or equal to the number.
  • The ceil function will return 0 if we pass 0 as an argument.
  • If the number which is passed as an argument is equal to the mathematical integer, then the result of the ceil function will be the same as the number passed as an argument.
  • Any number which is less than 0 but greater than -1.0, will result in 0.
    Also see,  Tribonacci Series and  Short int in C Programming

    Must Read Decision Making in C

FAQs

  1. What is the difference between ceil and floor function?
    A ceil function returns the smallest integer greater than or equal to the given number, whereas the floor function returns the largest number smaller than or equal to the given number.
     
  2. Name a few functions which are present in the math.h header file.
    Functions like sqrt(number): returns the square root of given number, pow(base, exponent): returns the power of given number, abs(number): returns the absolute value of given number, etc.
     
  3. What will be the output of the ceil function if we pass 0 as the argument.
    The ceil function will return 0, if we pass 0 as the argument.

Key Takeaways

In this article, we have extensively discussed what ceil function is, how to use it, and its implementation in Visual Studio Code.

Also read reverse a number.

We hope that this blog has helped you enhance your knowledge regarding the Ceiling function, and if you would like to learn more, check out our articles on different functions in STL, how to implement graphs using STL, understanding type casting, and type conversion in C++, etc. 

Do upvote our blog to help other ninjas grow. Happy Coding!”

Live masterclass