What is the purpose of the div() library function?

From the C99 Rationale document:

(7.20.6.2 The div, ldiv, and lldiv functions) Because C89 had implementation-defined semantics for division of signed integers when negative operands were involved, div and ldiv, and lldiv in C99, were invented to provide well-specified semantics for signed integer division and remainder operations. The semantics were adopted to be the same as in Fortran. Since these functions return both the quotient and the remainder, they also serve as a convenient way of efficiently modeling underlying hardware that
computes both results as part of the same operation. […]
Now that C99 requires similar semantics for the division operator, the main reason for new
programs to use div, ldiv or lldiv is to simultaneously obtain quotient and remainder.

Leave a Comment