div()

Computes the integer quotient and remainder.

  #include <stdlib.h>
  
  div_t div(int numer, int denom);    
Parameter

numer

The numerator.

denom

The denominator.

Remarks

The div_t type is defined in div_t.h as

  typedef struct { int quot, rem; } div_t;  
  

div() divides denom into numer and returns the quotient and remainder as a div_t type.

Listing: Example of div() usage

#include <stdlib.h>

#include <stdio.h>

int main(void)

{

div_t result;

ldiv_t lresult;

int d = 10, n = 103;

long int ld = 1000L, ln = 1000005L;

result = div(n, d);

lresult = ldiv(ln, ld);

printf("%d / %d has a quotient of %d\n",

n, d, result.quot);

printf("and a remainder of %d\n", result.rem);

printf("%ld / %ld has a quotient of %ld\n",

ln, ld, lresult.quot);

printf("and a remainder of %ld\n", lresult.rem);

return 0;

}

Output:

103 / 10 has a quotient of 10

and a remainder of 3

1000005 / 1000 has a quotient of 1000

and a remainder of 5