View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Ceil() Function in C: Syntax, Use Cases, and Examples

Updated on 23/04/20254,690 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.

Interested in making a career in programming? If so, pursue online Software Development courses from top universities!

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.

Here’s a concise answer.

What is ceil() Function in C?

The ceil() function in C is a mathematical function used to round a floating-point number up to the nearest integer that is greater than or equal to the given value. It is part of the math.h library and returns a double.

Must Explore: Introduction to C Tutorial!

Type-Specific Variants

C provides two additional variants of the ceil() function to support other floating-point types:

  1. ceill() – For long double values:
long double ceill(long double arg);

This variant is designed to handle long double inputs and ensures precision for extended floating-point values.

  1. ceilf() – For float values:
float ceilf(float arg);

This version is optimized for float inputs and offers better performance for single-precision operations.

Also Explore: Input and Output Function in C Programming article.

Syntax of ceil() in C

Here is the syntax of ceil() function in C:

#include <math.h>

double ceil(double x);

Parameter: A floating-point number (x) to round up.Return value: The smallest integer value greater than or equal to x, returned as a double.

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

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.

Must Read: Static Function in C: Definition, Examples & Real-World Applications

Example of ceil() Function 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 in C 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;
double result;

// Use ceil() function to round up to the nearest integer
result = ceil(num);

printf("The ceiling integer of %.2f = %.0f\n", 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 = 3.1;
float f2 = -3.8;
float f3 = 5.7;
float f4 = 48.4;
float f5 = 6.2;

// Use ceil() function to return the nearest integer greater than or equal to the 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

Are you aware of the Toupper Function? If not, read the Toupper Function in C article.

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
double num, result;

// Prompt the user for input
printf("Enter a floating-point number: ");
scanf("%lf", &num); // Use %lf for double

// Use ceil() function to round up
result = ceil(num);

// Display the result
printf("The ceiling of %.2f is %.0f\n", num, result);

return 0;
}

Sample Output:

Enter a floating-point number: 57.40
The ceiling of 57.40 is 58

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

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

int main() {
// Declare variables for positive and negative inputs
double num1, num2, result1, result2;

// Prompt user for input
printf("Enter a positive floating-point number: ");
scanf("%lf", &num1);

printf("Enter a negative floating-point number: ");
scanf("%lf", &num2);

// Use ceil() to round up both numbers
result1 = ceil(num1);
result2 = ceil(num2);

// Display the ceiling results
printf("\nThe ceiling of %.2f is %.0f", num1, result1);
printf("\nThe ceiling of %.2f is %.0f\n", num2, result2);

return 0;
}

Sample Output:

Enter a positive floating-point number: 5.6
Enter a negative floating-point number: -9.8
The ceiling of 5.60 is 6
The ceiling of -9.80 is -9

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'.

Here's a C program that demonstrates how the ceil() function in C handles different parameter types (like int, float, long long int) by implicitly converting them to double. It also shows that incompatible types like strings will result in compile-time errors (commented out to avoid actual error during compilation).

Example: Demonstrating Parameters of ceil() in C

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

int main() {
// Declare variables of various numeric types
int a = 5;
float b = 6.8f;
long long int c = 7;
double d = 9.3;

// Apply ceil() - all values will be implicitly converted to double
printf("Ceiling of int (%d): %.0f\n", a, ceil(a));
printf("Ceiling of float (%.2f): %.0f\n", b, ceil(b));
printf("Ceiling of long long int (%lld): %.0f\n", c, ceil(c));
printf("Ceiling of double (%.2f): %.0f\n", d, ceil(d));

// Uncommenting the line below will cause a compile-time error:
// char* str = "10.5";
// printf("Ceiling of string: %.0f\n", ceil(str)); // Invalid: incompatible type

return 0;
}

Sample Output:

Ceiling of int (5): 5
Ceiling of float (6.80): 7
Ceiling of long long int (7): 7
Ceiling of double (9.30): 10

Note:

  • ceil() can accept any numeric type, but C automatically promotes it to double.
  • Non-numeric types like char* (string) will throw compile-time errors, as they cannot be implicitly cast to double.

Must Read: C Function Call Stack article!

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.

Check out the Executive Diploma in Data Science & AI with IIIT-B!

Here's a clear and well-commented C program that demonstrates the return value of ceil() in C, showing how it returns a value of type double and how it can be stored in different data types.

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

int main() {
// Declare a double input value
double value = 132.67;

// Store the ceil result in different data types
double ceilDouble = ceil(value); // Recommended way (exact type)
int ceilInt = (int)ceil(value); // Explicit cast to int
unsigned long long int ceilULL = (unsigned long long int)ceil(value); // Cast to large integer type

// Print results to observe type behavior
printf("Original value: %.5f\n", value);
printf("Ceiling (stored as double): %.6f\n", ceilDouble);
printf("Ceiling (stored as int): %d\n", ceilInt);
printf("Ceiling (stored as unsigned long long int): %llu\n", ceilULL);

return 0;
}

Example: Return Value of ceil() in C

#include <stdio.h>

#include <math.h>

int main() {
// Declare a double input value
double value = 132.67;

// Store the ceil result in different data types
double ceilDouble = ceil(value); // Recommended way (exact type)
int ceilInt = (int)ceil(value); // Explicit cast to int
unsigned long long int ceilULL = (unsigned long long int)ceil(value); // Cast to large integer type

// Print results to observe type behavior
printf("Original value: %.5f\n", value);
printf("Ceiling (stored as double): %.6f\n", ceilDouble);
printf("Ceiling (stored as int): %d\n", ceilInt);
printf("Ceiling (stored as unsigned long long int): %llu\n", ceilULL);

return 0;
}

Sample Output:

Original value: 132.67000
Ceiling (stored as double): 133.000000
Ceiling (stored as int): 133
Ceiling (stored as unsigned long long int): 133

Explanation:

  • ceil() always returns a double like 133.000000, even though the result is an integer.
  • Storing the result in an int or unsigned long long int is fine via explicit casting, but the recommended way is to keep it in a double to avoid overflow or truncation issues, especially with large numbers.
  • double is used because it supports a much wider range than int.

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. 

FAQs

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.

What is the ceil value?

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

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.

What is the ceil() function in C?

The ceil() function in C is a math function used to round any floating-point number up to the nearest integer. It is available in the math.h library and returns a double value.

Which header file is required for ceil()?

To use the ceil() function in C, you must include the math.h header file. This library provides access to various mathematical functions, including ceil(), floor(), pow(), sqrt(), and many more.

Can I use ceil() with an int type?

Yes, you can pass an int to ceil(), but it gets automatically converted to double. Since integers don't have decimal parts, the return value will still be the same integer in double form, like 5.000000.

How does ceil() behave with negative numbers?

When used with negative numbers, the ceil() function returns the smallest integer greater than or equal to the input. For instance, ceil(-3.7) will return -3.0, which is closer to zero than -4.

Is ceil() different from rounding?

Yes, ceil() always rounds a number up, while general rounding may go up or down depending on the decimal. For example, rounding 2.3 gives 2, but ceil(2.3) always returns 3.

What data types are accepted by ceil()?

The ceil() function accepts double as a parameter. However, C automatically promotes float, int, or long long int to double. Non-numeric types like strings will throw a compile-time error.

What is the return value of ceil() in C?

The ceil() function in C always returns a result as a double, even if the output looks like an integer. This ensures accuracy and prevents overflow with large floating-point inputs.

Can I store ceil() results in an int?

Yes, but you must typecast the result manually. Since ceil() returns a double, storing it in an int without casting will cause a warning. Always use (int)ceil(x) when you need an int.

Why does ceil() return a double for an integer result?

Even when the result is a whole number, ceil() returns it in double format. This is because double supports a much wider range than int, reducing the risk of overflow in large calculations.

Is ceil() faster than round()?

In terms of performance, ceil() and round() are very similar for most basic applications. However, ceil() is specifically optimized for rounding up, making it more precise when that behavior is needed.

What are the alternatives to ceil() function in C?

Alternatives include floor() for rounding down and round() for the nearest integer. You can also use manual logic, but using built-in functions like ceil() is safer and more readable in C.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
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.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.