There is a function called div in C,C++ (stdlib.h)
div_t div(int numer, int denom);
typedef struct _div_t
{
int quot;
int rem;
} div_t;
But C,C++ have / and % operators.
My question is: “When there are / and % operators, Is div function useful?”
The div() function returns a structure which contains the quotient and remainder of the division of the first parameter (the numerator) by the second (the denominator). There are four variants:
div_t div(int, int)ldiv_t ldiv(long, long)lldiv_t lldiv(long long, long long)imaxdiv_t imaxdiv(intmax_t, intmax_t(intmax_t represents the biggest integer type available on the system)The
div_tstructure looks like this:The implementation does simply use the
/and%operators, so it’s not exactly a very complicated or necessary function, but it is part of the C standard (as defined by [ISO 9899:201x][1]).See the implementation in GNU libc: