Try this out:

#define CEILING_POS(X) ((X-(int)(X)) > 0 ? (int)(X+1) : (int)(X))
#define CEILING_NEG(X) (int)(X)
#define CEILING(X) ( ((X) > 0) ? CEILING_POS(X) : CEILING_NEG(X) )

Check out the link for comments, proof and discussion: http://www.linuxquestions.org/questions/programming-9/ceiling-function-c-programming-637404/

Thanks to Vilhelm Gray and carveone for pointing out that the linked definition of CEILING_NEG(X) is incorrect.

Answer from nintendo on Stack Overflow
🌐
Programiz
programiz.com › c-programming › library-function › math.h › ceil
C ceil() - C Standard Library
#include <stdio.h> #include <math.h> int main() { double num = 8.33; int result; result = ceil(num); printf("Ceiling integer of %.2f = %d", num, result); return 0; } ... Your builder path starts here. Builders don't just know how to code, they create solutions that matter.
Discussions

Writing a ceiling function in C
The variables number and value are both ints. Therefore the expression number/value is integer division: it is the division of two int values. This is integer division, and it returns an integer value, regardless of whether you subsequently decide to store that integer value in a float variable or an int variable. To get floating-point division, you need to cast at least one of the two variables number and value to float before you do the division. For example, float a = (float)number / value; Here, number is cast to a float before the division. Because one of the operands of the division is a floating-point value now, the division is floating-point division, not integer division. === For what it's worth, why aren't you just doing this? #include int ceiling(int number, int value) { return ceil((float)number / value); } More on reddit.com
🌐 r/learnprogramming
4
5
September 18, 2013
How to return the ceiling of a number without using ceil() function?
double ceil(double a) { auto b = double((long long)a); if (a < 0 || b == a) { return b; } else { return b+1; } } EDIT: fixed negative number behavior and casted to larger to integral type per u/bad_investor13 More on reddit.com
🌐 r/cpp_questions
22
5
September 12, 2023
In C, is it possible to emulate the ceil() function without utilizing ceil() nor if and else statements?
Why? https://en.cppreference.com/w/c/numeric/math/ceil There's a sample implementation there More on reddit.com
🌐 r/programminghelp
6
3
December 9, 2020
How to round up the answer of an integer division?
You could add 1 to the numerator before dividing ,i.e. (n+1)/2 In general, add (denominator - 1) More on reddit.com
🌐 r/C_Programming
26
33
May 26, 2020
People also ask

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.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › ceil function in c
Ceil() Function in C: Syntax, Usage & Examples
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.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › ceil function in c
Ceil() Function in C: Syntax, Usage & Examples
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.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › ceil function in c
Ceil() Function in C: Syntax, Usage & Examples
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_ceil.htm
C library - ceil() function
After executing the code, we get the following result − · Table of Ceiling Integers: Ceil(1.50) = 2 Ceil(2.50) = 3 Ceil(3.50) = 4 Ceil(4.50) = 5 Ceil(5.50) = 6 Ceil(6.50) = 7 Ceil(7.50) = 8 Ceil(8.50) = 9 Ceil(9.50) = 10 Ceil(10.50) = 11 · In ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-ceil-function
C ceil() Function - GeeksforGeeks
July 7, 2024 - The ceil() function returns the smallest integer value greater than or equal to the input number as a double. Input: double number = 2.9; Output: The floor value of 2.9 is 3 · The given below program demonstrates how we can calculate the ceil ...
🌐
W3Schools
w3schools.com › c › ref_math_ceil.php
C Math ceil() Function
C Examples C Real-Life Examples ... C Certificate ... printf("%f", ceil(0.60)); printf("%f", ceil(0.40)); printf("%f", ceil(5)); printf("%f", ceil(5.1)); printf("%f", ceil(-5.1)); printf("%f", ceil(-5.9)); Try it Yourself ...
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › ceil
ceil, ceilf, ceill - cppreference.com
May 23, 2024 - #include <math.h> #include <stdio.h> int main(void) { printf("ceil(+2.4) = %+.1f\n", ceil(2.4)); printf("ceil(-2.4) = %+.1f\n", ceil(-2.4)); printf("ceil(-0.0) = %+.1f\n", ceil(-0.0)); printf("ceil(-Inf) = %+f\n", ceil(-INFINITY)); }
Find elsewhere
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › ceil.php
C Language: ceil function (Ceiling)
The ceil function returns the smallest ... the ceil of value */ result = ceil(value); /* Display the result of the calculation */ printf("The ceil of %f is %f\n", value, result); return 0; }...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › ceil function in c
Ceil() Function in C: Syntax, Usage & Examples
January 4, 2026 - 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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › ceil-ceilf-ceill
ceil, ceilf, ceill | Microsoft Learn
July 9, 2025 - If you use the <tgmath.h> ceil() macro, the type of the argument determines which version of the function is selected. See Type-generic math for details. By default, this function's global state is scoped to the application. To change this state, see Global state in the CRT. For more compatibility information, see Compatibility. See the example for floor.
🌐
Tutorial Gateway
tutorialgateway.org › c-ceil-function
C ceil Function
April 2, 2025 - #include <stdio.h> #include <math.h> int main() { printf("\n The Ceil Value of 0.75 = %.2f ", ceil(0.75)); printf("\n The Ceil Value of 12.25 = %.2f ", ceil(12.25)); printf("\n The Ceil Value of 152.50 = %.2f ", ceil(152.50)); printf("\n The ...
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re31.html
ceil - C in a Nutshell [Book]
December 16, 2005 - /* Amount due = unit price * count ... is $99.99 double vat_rate = 0.055; // Value-added tax of 5.5% total = div( (int)ceil( (count * price) * (1 + vat_rate)), 100); printf("Total due: $%d.-\n", total.quot, total.rem);...
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
IncludeHelp
includehelp.com › c-programs › c-basic-programs-to-demonstrate-example-of-floor-and-ceil-functions.aspx
Example of floor() and ceil() functions in c
/* C program to demonstrate example of floor and ceil functions.*/ #include <stdio.h> #include <math.h> int main() { float val; float fVal,cVal; printf("Enter a float value: "); scanf("%f",&val); fVal=floor(val); cVal =ceil(val); printf("floor value:%f \nceil value:%f\n",fVal,cVal); return 0; }
🌐
Quora
quora.com › What-is-the-use-of-ceil-in-C-with-examples
What is the use of ceil in C, with examples? - Quora
Answer (1 of 5): C Language: ceil ... nearest integer). Syntax The syntax for the ceil function in the C Language is: [code]double ceil(double x);......
🌐
W3Resource
w3resource.com › c-programming › math › c-ceil.php
C ceil() function
December 24, 2022 - #include <math.h> #include <stdio.h> int main(void) { double x, y, z; x = 123.345; y = 1.05; z = -1.05; printf("Before applying ceil():"); printf("\nx = %.3f ; y = %.2f ; z = %.2f\n", x, y, z); x = ceil(123.345); y = ceil(1.05); z = ceil(-1.05); ...
🌐
Programming Simplified
programmingsimplified.com › c › math.h › ceil
ceil in C - math.h | Programming Simplified
January 6, 2022 - Home » C programming » math.h » ceil in C - math.h · Ceil function is used to round up a number i.e. it returns the smallest number which is greater than argument passed to it.
🌐
Scaler
scaler.com › topics › ceil-function-in-c
Ceil Function in C - Scaler Topics
April 20, 2022 - In this case, ceil function could be helpful. Like if Number = 45.67, then ceil function will return 46, which is an upper bound integral value. The syntax of the ceil() function in C is as below: ... The ceil function in C accepts a single parameter of double as a data type.
🌐
Reddit
reddit.com › r/learnprogramming › writing a ceiling function in c
r/learnprogramming on Reddit: Writing a ceiling function in C
September 18, 2013 -

I'm trying to write a function that (kind of) emulates the ceiling function in the math library, in a way that is more useful for my purposes. The code I've written is not working as expected. It should take two integer arguments, divide one by the other, and round up to the nearest integer no matter what the result is.

Ex: 2/2=1 --> answer is 1; 10/3=3.333... --> round up to 4.

This is my code:

int ceiling(int number, int value){
    float a = number/value;
    int b = number/value;
    float c = a - b;
    if(c!=0){
        b=b+1;
    }
    return b;
}

Using the examples above,

ceiling(2,2); would return 1, as expected.

ceiling(10,3); returns 3, when it should return 4.

My thought process is that if a is a float, then 10/3 should be 3.33333..., and if b is an int, then 10/3 should be 3. If c is a float, a-b should be 0.33333..., then since c!=0, ceiling should increment b and return that value. Where am I going wrong?

🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – ceil() function
C ceil() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - C ceil() function:ceil( ) function in C returns nearest integer value which is greater than or equal to the argument passed to this function.
🌐
CodeToFun
codetofun.com › c › math-ceil
C ceil() Function | CodeToFun
November 27, 2024 - In this example, the ceil() function is used to round up the floating-point number 8.45 to the nearest integer. The ceil() function returns a double value representing the smallest integer value that is greater than or equal to the given argument x. The primary use case for the ceil() function ...