Name

imaxdiv

Synopsis

Performs integer division, returning quotient and remainder

#include <inttypes.h>
imaxdiv_timaxdiv( 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.

Example

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;
else
  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()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.