C Tutorial



C Tutorial

Ceil Function in C

When it comes to mathematical operations in programming, the "ceil()" function in C is a handy tool for beginners to learn. Short for "ceiling", this function allows you to round up a given decimal number to the nearest whole number that is equal to or greater than the original value. 

Imagine you have a fraction like 3.2 and need 4 instead of 3. You can achieve that by using the "ceil()" function. It's beneficial in currency conversions, where you want to ensure precision. So, whether you're calculating prices, measurements, or any other situation requiring precise rounding up, C's "ceil()" function can be a perfect tool.

Another example is, consider a scenario where you need to find a given number's nearest integer upper bound. Here, the "ceil()" function comes to your aid. If you have a number like 45.48, applying the ceil function would result in 46, representing the rounded-up integral value—the closest integer upper bound to the original number.

Syntax of ceil() in C

Following would be the syntax of ceil() function in C:

double ceil(double x);

The ceil() function accepts a single argument and returns an integer value. If you pass 3.8 to Ceil (), it will give you 4.

What is ceil() in C?

The math library in C encompasses a range of functions that perform mathematical calculations on different types of operands. One such valuable function is ceil(). It takes a single parameter and provides the nearest integral value greater than or equal to the provided parameter as a result.

1. When the parameter type is long double, there is a related prototype called ceill() that can be utilised. 

long double ceill( long double arg );

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

float ceilf( float arg );

This ceil() function variant is specifically designed to handle long double data types. It ensures accurate rounding to the nearest greater or equal integral value for such parameters.

How To Use Ceil Function in C?

Using the ceil() function in C is quite straightforward. Here's a step-by-step guide on how to use it:

  • Include the math.h library in your C program by adding the line #include <math.h> at the beginning of your code. This library provides the declaration for the ceil() function.

  • Declare a variable of type double or float to store the decimal number you want to round up.

  • Assign a value to the variable, representing the decimal number you wish to round up.

  • To use the ceil() function, pass the variable as an argument within the parentheses of the ceil() function.

  • The ceil() function will return the rounded-up integer value.

  • You can store the result in another variable or use it directly in your program.

Example of ceil() in C

The ceil() function in the C header file is:

#include <math.h>

In the C Language, the ceil function can be used in the–  ANSI/ISO 9899-1990 version. 

Let’s look at the basic example of ceil() function:

#include <stdio.h>
#include <math.h>
int main()
   double num = 9.34;
   int result;
   result = ceil(num);
   printf("Ceiling integer of %.2f = %d", num, result);
   return 0;

When compiled and run, it will show this output:

Ceiling integer of 9.34 = 10

Let’s understand ceil function with the help of more examples:

Program 1: Ceil a value using the ceil() function

#include <stdio.h>  
#include <math.h>  
int main ()  
// declare local variables  
double num = 7.57;  
int result; 
// use ceil () function to round up the nearest value.   
result = ceil (num); 
printf ( " \n The ceiling integer of %.2f = %d ", num, result);  
return 0;     

This is the final output:

The ceiling integer of 7.57 = 8

In the given program, the ceil() function is utilised with the variable num, which holds the value 7.57. When the ceil() function is applied, it rounds off the number to its closest integer value, resulting in 8.

Program 2: Ceil the floating value using the ceil() function

#include <stdio.h>  
#include <math.h>  
int main ()  
// declare local variables  
float f1, f2, f3, f4, f5;  
f1 = 3.1;  
f2 = -3.8;  
f3 = 5.7;  
f4 = 48.4;  
f5 = 6.2;   
// use ceil() function to return the nearest number  
printf (" The ceiling value of %.2f is %.2f \n ", f1, ceil (f1));  
printf (" The ceiling value of %.2f is %.2f \n ", f2, ceil (f2));  
printf (" The ceiling value of %.2f is %.2f \n ", f3, ceil (f3));  
printf (" The ceiling value of %.2f is %.2f \n ", f4, ceil (f4));  
printf (" The ceiling value of %.2f is %.2f \n ", f5, ceil (f5));  
return 0;  

This is the final output:

The ceiling value of 3.10 is 4.00
The ceiling value of -3.80 is -3.00
The ceiling value of 5.70 is 6.00
The ceiling value of 48.40 is 49.00
The ceiling value of 6.20 is 7.00

Program 3: Round up the floating digit using the ceil() function

Here are the key points regarding the round-off technique and the usage of the ceil() function in C:

  • Using the round-off technique helps assess if the value of the fraction after the decimal point is more than 0.5.

  • If the value exceeds 0.5, the number is assumed to be closer to the following integral value.

  • If the fractional value is lower than 0.5, the number is nearer to the same/initial integral value.

  • To round off a number using the ceil() function, we subtract 0.5 from the original value.

  • If the resulting value still contains a fractional part, ceil() will return the next integral value.

  • If the resulting value becomes integral, ceil() will return that exact value.

  • When the fractional value equals 0.5, subtracting 0.5 will result in an integral value, which ceil() will return.

  • This technique ensures that the ceil() function rounds numbers up to their nearest integral values based on the fractional part.

#include <stdio.h>  
#include <math.h>  
int main ()  
// declare local variables  
float num;  
float result;  
printf (" Enter the float number: ");  
scanf ( "%f", &num);   
// use ceil () function  
result = ceil (num);  
printf ( " The ceil of %.2f is %f ", num, result);  
return 0;  

This is the final output:

Enter the float number: 57.40
The ceil of 57.40 is 58.000000

Program 4: Round up the positive and negative values using ceil() 

#include <stdio.h>  
#include <math.h>  
int main ()  
int num, result, num2, result2;  
printf (" Enter the positive integer: ");  
scanf (" %d", &num);  
printf (" Enter the negative integer: ");  
scanf (" %d", &num2);  
// use ceil () function to round up the nearest value of the given number. */  
result = ceil(num);  
result2 = ceil(num2);  
printf ( " \n The ceiling integer of %d = %d ", num, result);  
printf ( " \n The ceiling integer of %d = %d ", num2, result2);  
return 0;  

This is the final output:

Enter the positive integer: 57
Enter the negative integer: -25

The ceiling integer of 57 = 57
The ceiling integer of -25 = -25

Parameters of ceil() in C

The ceil() function in C specifically requires a single parameter of type double. The function will internally perform typecasting if the program attempts to pass a different data type, such as int, float, or long long int. This means the argument will be converted to the double data type before being processed by the ceil function. However, if a data type, like a string, cannot be implicitly converted to double, a compile-time error will occur, indicating incompatibility.

error: incompatible type for the argument of 'ceil'.

Return Value of ceil() in C

The ceil() function in C returns an integer’s value as a double data type, such as 25.000000 or 132.000000. While double is typically used to store floating-point numbers with fractional parts, like 25.67354, it is used as a return type for ceil() in C due to its significantly large range, which enables users to eliminate potential overflow and bring accurate results to the table. On the other hand, users can surely use int or even unsigned long long int to store ceil results, though values included can be very limited.


In a nutshell, we have learned that the ceil() function in C returns an integral value of the double data type. This might seem unusual since double is commonly associated with fractional values. However, the reason for using double as the return type is its vast range, surpassing that of int or unsigned long long int. 

Check out upGrad’s Master of Science in Computer Science offered by Liverpool John Moores University to further grasp the intricacy and significance of different programming languages. With this course, students can learn in-demand skills and elevate their software development careers.

Enroll now to start your journey!


1. What is floor () vs ceil ()?

The Math.ceil method provides the smallest integer as a result, which is higher than or equals the given value, while Math.floor offers the largest integer as a result that is less than or equal to the provided value. In simple terms, floor() rounds down while ceil () rounds up to the closest integer value. 

2. What is the ceil value? 

The ceil() function calculates the next highest whole number greater than or equal to x.

3. How do you use ceil and floor?

The floor and ceiling functions round a decimal number to the nearest integer. For instance, the floor and ceiling of 3.31, respectively, are 3 and 4. This allows us to locate the closest integer to a decimal number on a number line.

Leave a Reply

Your email address will not be published. Required fields are marked *