1DIV(3) Linux Programmer's Manual DIV(3)
2
3
4
6 div, ldiv, lldiv, imaxdiv - compute quotient and remainder of an inte‐
7 ger division
8
10 #include <stdlib.h>
11
12 div_t div(int numerator, int denominator);
13 ldiv_t ldiv(long numerator, long denominator);
14 lldiv_t lldiv(long long numerator, long long denominator);
15
16 #include <inttypes.h>
17
18 imaxdiv_t imaxdiv(intmax_t numerator, intmax_t denominator);
19
20 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22 lldiv():
23 _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
24
26 The div() function computes the value numerator/denominator and returns
27 the quotient and remainder in a structure named div_t that contains two
28 integer members (in unspecified order) named quot and rem. The quo‐
29 tient is rounded toward zero. The result satisfies quot*denomina‐
30 tor+rem = numerator.
31
32 The ldiv(), lldiv(), and imaxdiv() functions do the same, dividing num‐
33 bers of the indicated type and returning the result in a structure of
34 the indicated name, in all cases with fields quot and rem of the same
35 type as the function arguments.
36
38 The div_t (etc.) structure.
39
41 For an explanation of the terms used in this section, see at‐
42 tributes(7).
43
44 ┌────────────────────────────────────────────┬───────────────┬─────────┐
45 │Interface │ Attribute │ Value │
46 ├────────────────────────────────────────────┼───────────────┼─────────┤
47 │div(), ldiv(), lldiv(), imaxdiv() │ Thread safety │ MT-Safe │
48 └────────────────────────────────────────────┴───────────────┴─────────┘
49
51 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD. The functions ll‐
52 div() and imaxdiv() were added in C99.
53
55 After
56
57 div_t q = div(-5, 3);
58
59 the values q.quot and q.rem are -1 and -2, respectively.
60
62 abs(3), remainder(3)
63
65 This page is part of release 5.12 of the Linux man-pages project. A
66 description of the project, information about reporting bugs, and the
67 latest version of this page, can be found at
68 https://www.kernel.org/doc/man-pages/.
69
70
71
72 2021-03-22 DIV(3)