国际访客建议访问 Primers 编程伙伴 国际版站点 > C 教程 > imaxdiv 以获得更好的体验。

# C 语言标准库函数 imaxdiv

在头文件 inttypes.h 中定义。
/*********************************************
 * @brief 计算除法,同时得到商和余数
 * @param x 被除数
 * @param y 除数
 * @return 包含商和余数的结构体
 ********************************************/
imaxdiv_t imaxdiv(intmax_t x, intmax_t y);

!subtitle:说明

计算 intmax_t 类型的除法,同时得到商和余数,其中商向零取整

!subtitle:参数

  • x - 被除数

  • y - 除数

!subtitle:返回值

包含商和余数的结构体。

结构体定义可能为(字段顺序未定义):

struct imaxdiv_t {
    intmax_t quot;   // 商
    intmax_t rem;    // 余数
};

struct imaxdiv_t {
    intmax_t rem;    // 余数
    intmax_t quot;   // 商
};

# 示例

#include <stdio.h>
#include <inttypes.h>

int main(void)
{
    imaxdiv_t result = imaxdiv(10, 3);  // 得 3 余 1
    printf("imaxdiv(10, 3) = %" PRIdMAX " ... %" PRIdMAX"\n", result.quot, result.rem);

    result = imaxdiv(-10, 3);  // 得 -3 余 -1
    printf("imaxdiv(-10, 3) = %" PRIdMAX " ... %" PRIdMAX"\n", result.quot, result.rem);
    
    return 0;
}

运行结果:

imaxdiv(10, 3) = 3 ... 1
imaxdiv(-10, 3) = -3 ... -1

# 推荐阅读

# 参考标准

  • C23 standard (ISO/IEC 9899:2024):

    • 7.8.2.2 The imaxdiv function (p: TBD)

    • 7.22.6.2 The div, ldiv and lldiv functions (p: TBD)

  • C17 standard (ISO/IEC 9899:2018):

    • 7.8.2.2 The imaxdiv function (p: 159)

    • 7.22.6.2 The div, ldiv and lldiv functions (p: 259)

  • 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)

本文 更新于: 2025-11-27 09:38:08 创建于: 2025-11-27 09:38:08