(ANSI Standard)
#include <stdlib.h> result = div(top,bottom);
The "div" function performs the integer division of "top" divided by "bottom". The result of "div" has a structure type named "div_t", defined in <stdlib.h> with the members
int quot; /*quotient*/ int rem; /*remainder*/
The "quot" element is the integer quotient of the division and the "rem" element is the remainder. The remainder always has the same sign as the result of the division (which is not true of A%B in some implementations). The quotient is defined so that
top == quot*bottom + rem
When the division is inexact, this means that the quotient is always adjusted towards 0. Thus
div_t x; x = div(9,5); printf("%d\n",x.quot); /* prints 1 */ x = div(-9,5); printf("%d\n",x.quot); /* prints -1 */
If the result of the division cannot be represented, the behavior is undefined.
Copyright © 1996, Thinkage Ltd.