DIV(S) UNIX System V DIV(S)
Name
div - divides integers
Syntax
#include <stdlib.h>
struct div_t {
int quot; /* Quotient */
int rem; /* Remainder */
} div(numerator, denominator)
int numerator;
int denominator;
Description
The div function divides numerator by denominator,
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 zero, the program terminates 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.
See Also
ldiv(S)
Example
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main(argc, argv)
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);
}
The example above 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 .
Assuming the executable file is named tdiv, it might be
executed as:
tdiv 5 2
the output would read:
x is 5, y is 2
The quotient is 2, and the remainder is 1
Standards Conformance
div is conformant with:
ANSI X3.159-198X C Language Draft Standard, May 13, 1988.
(printed 6/20/89)