DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH

# div(S)

div -- divides integers

## Syntax

cc . . . -lc

```#include <stdlib.h>

struct div_t {
int quot;       /* Quotient   */
int rem;        /* Remainder  */
}

div_t 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.

See the example which follows.

## 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.

ldiv(S)

## Standards conformance

div is conformant with:

ANSI X3.159-1989 Programming Language -- C .

## 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);
y = atoi(argv);
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
```

```   x is 5, y is 2