C library function - ldiv()
Description
C Library Function div_t ldiv(long int numer, long int denom) Divides the **numer (numerator)** by denom (denominator), used to compute the quotient and remainder of two long integer values.
Declaration
Here is the declaration of the ldiv() function.
ldiv_t ldiv(long int numer, long int denom)
Parameters
- ** numer ** -- numerator.
- ** denom ** -- denominator.
Return Value
This function returns the value in a structure defined in
The purpose of the ldiv() function is to avoid repeated computation when both the quotient and the remainder are needed, since the remainder is obtained at the same time as the quotient. In addition, when the dividend or divisor is negative, the result of ldiv() follows the rule of rounding toward zero.
Example
The following example demonstrates the use of the ldiv() function.
Example
#include
#include
int
main
(
)
{
// The dividend is 11 and the divisor is 3
long
int
numer
=
11
;
long
int
denom
=
3
;
ldiv_t result
=
ldiv
(
numer
,
denom
)
;
printf
(
"quotient = %ld\n"
,
result.
quot
)
;
printf
(
"remainder = %ld\n"
,
result.
rem
)
;
return
0
;
}
Let's compile and run the above program, which will produce the following result:
商 = 3
余数 = 2
C Standard Library -