Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - ldiv()

C library function - ldiv()

C Standard Library - <stdlib.h> C Standard Library -

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

Return Value

This function returns the value in a structure defined in the values in a structure that has two members, such as ldiv_t:long quot; long rem;, representing the quotient and remainder of the division, respectively.

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 - <stdlib.h> C Standard Library -