例子:
/* 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.
*/
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:
x is 876, y is 13
The quotient is 67, and the remainder is 5
/* LDIV.C: This program takes two long integers
* as command-line arguments and displays the
* results of the integer division.
*/
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