6.19.22 lldiv Function
Calculates the quotient and remainder of two long integers.
Include
<stdlib.h>
Prototype
ldiv_t ldiv(long numer, long denom);
Arguments
numer
- numerator
denom
- denominator
Return Value
Returns the quotient and the remainder.
Remarks
The returned quotient will have the same sign as the numerator divided by the
denominator. The sign for the remainder will be such that the quotient times the
denominator plus the remainder will equal the numerator (quot * denom + rem =
numer
). If the denominator is zero, 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 <stdlib.h>
#include <stdio.h>
int main(void)
{
long long x, y;
lldiv_t z;
x = 7;
y = 3;
printf("For lldiv(%lld, %lld)\n", x, y);
z = lldiv(x, y);
printf("The quotient is %lld and the "
"remainder is %lld\n\n", z.quot, z.rem);
x = 7;
y = -3;
printf("For lldiv(%lld, %lld)\n", x, y);
z = lldiv(x, y);
printf("The quotient is %lld and the "
"remainder is %lld\n\n", z.quot, z.rem);
x = -5;
y = 3;
printf("For lldiv(%lld, %lld)\n", x, y);
z = lldiv(x, y);
printf("The quotient is %lld and the "
"remainder is %lld\n\n", z.quot, z.rem);
x = 7;
y = 7;
printf("For lldiv(%lld, %lld)\n", x, y);
z = lldiv(x, y);
printf("The quotient is %lld and the "
"remainder is %lld\n\n", z.quot, z.rem);
}
Example Output
For lldiv(7, 3)
The quotient is 2 and the remainder is 1
For lldiv(7, -3)
The quotient is -2 and the remainder is 1
For lldiv(-5, 3)
The quotient is -1 and the remainder is -2
For lldiv(7, 7)
The quotient is 1 and the remainder is 0