C 标准库 - <math.h>

C 语言 ceil() 函数用于返回不小于参数的最小整数(向上取整),返回 double。

原型

double ceil(double arg);

示例

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

int main() {
    printf("ceil(2.3) = %.0f\n", ceil(2.3));
    printf("ceil(-2.3) = %.0f\n", ceil(-2.3));
    return 0;
}

输出:

ceil(2.3) = 3
ceil(-2.3) = -2

ceil 朝正无穷方向取整,负数时 -2.3 向上取整为 -2。

注意事项

使用 math.h 的函数时,多数编译器需要链接数学库(编译选项 -lm,如 gcc test.c -lm -o test)。所有三角函数使用弧度制。