ldiv

Description

Computes the quotient and remainder of a long integer.

#include <stdlib.h>

ldiv_t ldiv ( long int numer, long int denom );

numer Numerator  
denom Denominator  

Remarks

The ldiv function divides numer by denom, computing the quotient and the remainder. The sign of the quotient is the same as that of the mathematical quotient. Its absolute value is the largest integer that is less than the absolute value of the mathematical quotient. If the denominator is 0, the program will terminate with an error message.

The ldiv function is similar to the div function, with the difference being that the arguments and the members of the returned structure are all of type long int.

The ldiv_t structure, defined in STDLIB.H, contains the following elements:

Element Description

long int quot Quotient
long int rem Remainder

Return Value

The ldiv function returns a structure of type ldiv_t, comprising both the quotient and the remainder.

Compatibility

Standards:ANSI

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

div

Example

/* LDIV.C: This program takes two long integers as command-line

* arguments and displays the results of the integer division.

*/

#include <stdlib.h>

#include <math.h>

#include <stdio.h>

void main( void )

{

long x = 5149627, y = 234879;

ldiv_t div_result;

div_result = ldiv( x, y );

printf( "For %ld / %ld, the quotient is ", x, y );

printf( "%ld, and the remainder is %ld\n",

div_result.quot, div_result.rem );

}

Output

For 5149627 / 234879, the quotient is 21, and the remainder is 217168