6.7.7 imaxdiv Function

Compute division and remainder of a greatest-width integer in one operation.

Include

<inttypes.h>

Prototype

imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom);

Arguments

numer
The numerator argument
denom
The denominator argument

Return Value

The imaxdiv function computes the division, numer / denom, and remainder, numer % denom, of the arguments and returns the results in an imaxdiv_t structure.

Remarks

If either part of the result cannot be represented, the behavior is undefined.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  intmax_t numer, denom;
  imaxdiv_t result;

  numer = 400;
  denom = 3;
  result = imaxdiv(numer, denom);
  printf("The remainder of %" PRIdMAX " divided by %" PRIdMAX " is %" PRIdMAX "\n", numer, denom, result.rem);
}

Example Output

The remainder of 400 divided by 3 is 1