div, ldiv, lldiv, imaxdiv

Defined in header <stdlib.h>
div_t     div( int x, int y );
ldiv_t    ldiv( long x, long y );
lldiv_t   lldiv( long long x, long long y );
(3) (since C99)
Defined in header <inttypes.h>
imaxdiv_t imaxdiv( intmax_t x, intmax_t y );
(4) (since C99)

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

Computes quotient and remainder simultaneously. The quotient is the algebraic quotient with any fractional part discarded (truncated towards zero). The remainder is such that quot * y + rem == x.

(until C99)

Computes the quotient (the result of the expression x/y) and remainder (the result of the expression x%y) simultaneously.

(since C99)


x, y - integer values

Return value

If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, imaxdiv_t, respectively), returns both as an object of type div_t, ldiv_t, lldiv_t, imaxdiv_t defined as follows:


struct div_t { int quot; int rem; };


struct div_t { int rem; int quot; };


struct ldiv_t { long quot; long rem; };


struct ldiv_t { long rem; long quot; };


struct lldiv_t { long long quot; long long rem; };


struct lldiv_t { long long rem; long long quot; };


struct imaxdiv_t { intmax_t quot; intmax_t rem; };


struct imaxdiv_t { intmax_t rem; intmax_t quot; };

If either the remainder or the quotient cannot be represented, the behavior is undefined.


Until C99, the rounding direction of the quotient and the sign of the remainder in the built-in division and remainder operators was implementation-defined if either of the operands was negative, but it was well-defined in div and ldiv.

On many platforms, a single CPU instruction obtains both the quotient and the remainder, and this function may leverage that, although compilers are generally able to merge nearby / and % where suitable.


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// demo only: does not check for buffer overflow
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; } // reverse
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);




  • C11 standard (ISO/IEC 9899:2011):
    • The imaxdiv function (p: 219)
    • The div, ldiv and lldiv functions (p: 356)
  • C99 standard (ISO/IEC 9899:1999):
    • The imaxdiv function (p: 200)
    • The div, ldiv and lldiv functions (p: 320)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 4.10 div_t, ldiv_t
    • The div function
    • The ldiv function

See also

computes remainder of the floating-point division operation
computes signed remainder of the floating-point division operation
computes signed remainder as well as the three last bits of the division operation

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.