Previous Page
Next Page


Performs integer division, returning quotient and remainder

#include <inttypes.h>
imaxdiv_t imaxdiv ( intmax_t dividend , intmax_t divisor  );

The imaxdiv( ) function is the same as either ldiv( ) or lldiv( ), depending on how many bits wide the system's largest integer type is. Accordingly, the structure type of the return value, imaxdiv_t, is the same as either ldiv_t or lldiv_t.


intmax_t people = 110284, apples = 9043291;
imaxdiv_t  share;

if ( people == 0 )              // Avoid dividing by zero.
    printf( "There's no one here to take the apples.\n" ), return -1;
  share = imaxdiv( apples, people );

printf( "If there are %ji of us and %ji apples,\n"
        "each of us gets %ji, with %ji left over.\n",
        people, apples, share.quot, share.rem );

This example prints the following output:

If there are 110284 of us and 9091817 apples,
each of us gets 82, with 3 left over.

See Also

The description under div( ) in this chapter; the floating point functions remainder( ) and remquo( )

Previous Page
Next Page