div

Description

Computes the quotient and the remainder of two integer values.

#include <stdlib.h>

div_t div( int numer, int denom );

numer Numerator  
denom Denominator  

Remarks

The div function divides numer by denom, computing the quotient and the remainder. The div_t structure contains the following elements:

Element Description

int quot Quotient
int rem 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.

Return Value

The div function returns a structure of type div_t, comprising both the quotient and the remainder. The structure is defined in STDLIB.H.

Compatibility

Standards:ANSI

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

32-Bit:DOS32X

See Also

ldiv

Example

/* DIV.C: This example takes two integers as command-line arguments and

* displays the results of the integer division. This program accepts

* two arguments on the command line following the program name, then

* calls div to divide the first argument by the second. Finally,

* it prints the structure members quot and rem.

*/

#include <stdlib.h>

#include <stdio.h>

#include <math.h>

void main( int argc, char *argv[] )

{

int x,y;

div_t div_result;

x = atoi( argv[1] );

y = atoi( argv[2] );

printf( "x is %d, y is %d\n", x, y );

div_result = div( x, y );

printf( "The quotient is %d, and the remainder is %d\n",

div_result.quot, div_result.rem );

}

Output

[C:\LIBREF] div 876 13

x is 876, y is 13

The quotient is 67, and the remainder is 5