C 参考手册
- C 语言
 - C 关键词
 - 预处理器
 - C 标准库头文件
 - 类型支持
 - 程序支持工具
 - 变参数函数
 - 错误处理
 - 动态内存管理
 - 日期和时间工具
 - 字符串库
 - 算法
 - 数值
 - 常用数学函数
 - MATH_ERRNO, MATH_ERREXCEPT, math_errhandling
 - log, logf, logl
 - log10, log10f, log10l
 - log1p, log1pf, log1pl
 - log2, log2f, log2l
 - cbrt, cbrtf, cbrtl
 - fmax, fmaxf, fmaxl
 - fmin, fminf, fminl
 - fdim
 - NAN
 - exp, expf, expl
 - exp2, exp2f, exp2l
 - expm1, expm1f, expm1l
 - fmod, fmodf, fmodl
 - remainder, remainderf, remainderl
 - remquo, remquof, remquol
 - fma, fmaf, fmal
 - div, ldiv, lldiv, imaxdiv
 - fabs, fabsf, fabsl
 - sqrt, sqrtf, sqrtl
 - sin, sinf, sinl
 - abs, labs, llabs, imaxabs
 - hypot, hypotf, hypotl
 - pow, powf, powl
 - cos, cosf, cosl
 - tan, tanf, tanl
 - asin, asinf, asinl
 - acos, acosf, acosl
 - atan, atanf, atanl
 - atan2, atan2f, atan2l
 - sinh, sinhf, sinhl
 - cosh, coshf, coshl
 - tanh, tanhf, tanhl
 - asinh, asinhf, asinhl
 - acosh, acoshf, acoshl
 - atanh, atanhf, atanhl
 - erf, erff, erfl
 - erfc, erfcf, erfcl
 - lgamma, lgammaf, lgammal
 - tgamma, tgammaf, tgammal
 - ceil, ceilf, ceill
 - floor, floorf, floorl
 - round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl
 - trunc, truncf, truncl
 - nearbyint, nearbyintf, nearbyintl
 - rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl
 - ldexp, ldexpf, ldexpl
 - scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl
 - ilogb, ilogbf, ilogbl
 - logb, logbf, logbl
 - frexp, frexpf, frexpl
 - modf, modff, modfl
 - nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl
 - copysign, copysignf, copysignl
 - fpclassify
 - isfinite
 - isinf
 - isnan
 - isnormal
 - signbit
 - isgreater
 - isgreaterequal
 - isless
 - islessequal
 - islessgreater
 - isunordered
 - float_t, double_t
 - HUGE_VALF, HUGE_VAL, HUGE_VALL
 - INFINITY
 - FP_NORMAL, FP_SUBNORMAL, FP_ZERO, FP_INFINITE, FP_NAN
 - 浮点环境
 - 伪随机数生成
 - 复数算术
 - 泛型数学
 - 文件输入/输出
 - 本地化支持
 - 原子操作库
 - 线程支持库
 - 实验性 C 标准库
 - 有用的资源
 - 符号索引
 - 注释
 
div, ldiv, lldiv, imaxdiv
|   定义于头文件  <stdlib.h>
  | 
||
|   div_t     div( int x, int y );  | 
(1) | |
|   ldiv_t    ldiv( long x, long y );  | 
(2) | |
|   lldiv_t   lldiv( long long x, long long y );  | 
(3) | (C99 起) | 
|   定义于头文件  <inttypes.h>
  | 
||
| (4) | (C99 起) | |
计算分子 x 除以分母 y 的商和余数。
| 
 同时计算商和余数。商为舍弃小数部分(向零取整)的代数商。余数满足 quot * y + rem == x 的值。  | 
(C99 前) | 
| 
 同时计算商(表达式 x/y 的结果)和余数(表达式 x%y 的结果)。  | 
(C99 起) | 
参数
| x, y | - | 整数值 | 
返回值
若余数和商都能表示成对应类型的对象(分别为 div_t 、 ldiv_t 、 lldiv_t 、 imaxdiv_t ),则将两者作为返回作为定义如下的 div_t 、 ldiv_t 、 lldiv_t 、 imaxdiv_t 类型对象返回:
div_t
struct div_t { int quot; int rem; };
或
struct div_t { int rem; int quot; };
ldiv_t
struct ldiv_t { long quot; long rem; };
或
struct ldiv_t { long rem; long quot; };
lldiv_t
struct lldiv_t { long long quot; long long rem; };
或
struct lldiv_t { long long rem; long long quot; };
imaxdiv_t
或
若余数或商无法表示,则行为未定义。
注意
C99 前,若运算数之一为负,则内建的除法和取余运算符中的商取整方向和余数符号是实现定义的,但它在 div 和 ldiv 中良好定义。
多数平台上,单条 CPU 指令获得商和余数,而此函数可以活用这点,尽管编译器通常能在适合处合并临近的 / 和 % 。
示例
运行此代码
#include <stdio.h> #include <math.h> #include <stdlib.h> // 只为演示:不检查缓冲区溢出 void itoa(int n, int base, char* buf) { div_t dv = {.quot = n}; char* p = buf; do { dv = div(dv.quot, base); *p++ = "0123456789abcdef"[abs(dv.rem)]; } while(dv.quot); if(n<0) *p++ = '-'; *p-- = '\0'; while(buf < p) { char c = *p; *p-- = *buf; *buf++ = c; } // 反转 } int main(void) { char buf[100]; itoa(12346, 10, buf); printf("%s\n", buf); itoa(-12346, 10, buf); printf("%s\n", buf); itoa(65535, 16, buf); printf("%s\n", buf); }
输出:
12346 -12346 ffff
引用
- C11 standard (ISO/IEC 9899:2011):
 
- 7.8.2.2 The imaxdiv function (p: 219)
 
- 7.22.6.2 The div, ldiv and lldiv functions (p: 356)
 
- C99 standard (ISO/IEC 9899:1999):
 
- 7.8.2.2 The imaxdiv function (p: 200)
 
- 7.20.6.2 The div, ldiv and lldiv functions (p: 320)
 
- C89/C90 standard (ISO/IEC 9899:1990):
 
- 4.10 div_t, ldiv_t
 
- 4.10.6.2 The div function
 
- 4.10.6.4 The ldiv function
 
参阅
|    (C99)(C99)  | 
   计算浮点除法运算的余数   (函数)  | 
|    (C99)(C99)(C99)  | 
   计算浮点除法运算的带符号余数   (函数)  | 
|    (C99)(C99)(C99)  | 
   计算除法运算的带符号余数,以及商的后三位   (函数)  |