The C div() function computes integer quotient and remainder together. The quotient truncates toward zero and the remainder has the same sign as the numerator when nonzero. The identity numerator = quot * denominator + rem holds.
This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. For a valid representable division, both results are returned in div_t. There is no failure sentinel; zero division or an unrepresentable quotient is undefined behavior. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete div() call:
C
div(17, 5);
Call
What it does
div(17, 5);
Computes integer quotient and remainder together using the concrete values shown.
Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.
Library declaration
C
div_t div(int numerator, int denominator);
Parameters
Parameter
Description
numerator
Dividend of type int.
denominator
Nonzero divisor of type int.
Return Value
Return Value
Description
div_t
Structure containing quot and rem int members.
C Examples
Example 1 — Quotient and remainder
This complete program demonstrates a distinct part of the div() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
div_t result = div(17, 5);
printf("quot = %d, rem = %d\n", result.quot, result.rem);
return 0;
}
Output
quot = 3, rem = 2
How It Works
Step 1. Supply a nonzero divisor.
Step 2. The function performs one integer division.
Step 3. Read quotient and remainder from the result structure.
Step 4. Verify 17 equals 3 times 5 plus 2.
Example 2 — Negative dividend
This complete program demonstrates a distinct part of the div() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
div_t result = div(-17, 5);
printf("%d %d\n", result.quot, result.rem);
return 0;
}
Output
-3 -2
How It Works
Step 1. Use a negative numerator.
Step 2. Division truncates the quotient toward zero.
Step 3. The remainder follows the numerator's sign.
Step 4. Both structure members are printed.
Example 3 — Split into groups
This complete program demonstrates a distinct part of the div() contract and checks the result or precondition that matters for the scenario.
div() computes integer quotient and remainder together. The quotient truncates toward zero and the remainder has the same sign as the numerator when nonzero. The identity numerator = quot * denominator + rem holds.
Include <stdlib.h> before calling div().
For a valid representable division, both results are returned in div_t. There is no failure sentinel; zero division or an unrepresentable quotient is undefined behavior.
The denominator must not be zero. INT_MIN divided by -1 is also not representable for typical int and must be avoided.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.