1. Home

Explore C Tutorials: From Beginner Concepts to Advanced Techniques

Dive into our C tutorials and expand your programming skills. From fundamental concepts to advanced techniques, we've got you covered. Start your journey to C mastery today

• 130 Lessons
• 22 Hours
30.Â

# Ceil Function in C

Updated on 19/07/20243,883 Views

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 `

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 #include 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 Â #include Â 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 Â #include Â 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.00The ceilingÂ valueÂ ofÂ -3.80Â isÂ -3.00The ceilingÂ valueÂ ofÂ 5.70Â isÂ 6.00The ceilingÂ valueÂ ofÂ 48.40Â isÂ 49.00The 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 Â #include Â 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.40The ceil of 57.40 is 58.000000`

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

 `#include Â #include Â 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: 57EnterÂ theÂ negativeÂ integer:Â -25The ceilingÂ integerÂ ofÂ 57Â =Â 57The 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.

## Conclusion

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!

## FAQs

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.

Test your C programming skills with a Free Quiz

Director of Engineering

Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working â€¦Read More

###### Need Guidance? We're Here to Help!
+91
*
By clicking, I accept theT&Cand
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.

Talk to our experts. Weâ€™re available 24/7.

Indian Nationals

1800 210 2020

Foreign Nationals

+918045604032

## Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...