For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
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.
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!
C provides two additional variants of the ceil() function to support other floating-point types:
long double ceill(long double arg);
This variant is designed to handle long double inputs and ensures precision for extended floating-point 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.
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:
Must Read: Static Function in C: Definition, Examples & Real-World Applications
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:
#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.
#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.
Here are the key points regarding the round-off technique and the usage of the ceil() function in C:
#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
#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
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:
Must Read: C Function Call Stack article!
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:
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.
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.
The ceil() function calculates the next highest whole number greater than or equal to x.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
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.