| 1 |
'\" t |
| 2 |
.\" Copyright 1993, David Metcalfe <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH div 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
div, ldiv, lldiv, imaxdiv \- compute quotient and remainder of |
| 10 |
an integer division |
| 11 |
.SH LIBRARY |
| 12 |
Standard C library |
| 13 |
.RI ( libc ,\~ \-lc ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.B #include <stdlib.h> |
| 17 |
.P |
| 18 |
.BI "div_t div(int " numerator ", int " denominator ); |
| 19 |
.BI "ldiv_t ldiv(long " numerator ", long " denominator ); |
| 20 |
.BI "lldiv_t lldiv(long long " numerator ", long long " denominator ); |
| 21 |
.P |
| 22 |
.B #include <inttypes.h> |
| 23 |
.P |
| 24 |
.BI "imaxdiv_t imaxdiv(intmax_t " numerator ", intmax_t " denominator ); |
| 25 |
.fi |
| 26 |
.P |
| 27 |
.RS -4 |
| 28 |
Feature Test Macro Requirements for glibc (see |
| 29 |
.BR feature_test_macros (7)): |
| 30 |
.RE |
| 31 |
.P |
| 32 |
.BR lldiv (): |
| 33 |
.nf |
| 34 |
_ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L |
| 35 |
.fi |
| 36 |
.SH DESCRIPTION |
| 37 |
The |
| 38 |
.BR div () |
| 39 |
function computes the value |
| 40 |
.I numerator/denominator |
| 41 |
and returns the quotient and remainder in a structure |
| 42 |
named |
| 43 |
.I div_t |
| 44 |
that contains |
| 45 |
two integer members (in unspecified order) named |
| 46 |
.I quot |
| 47 |
and |
| 48 |
.IR rem . |
| 49 |
The quotient is rounded toward zero. |
| 50 |
The result satisfies |
| 51 |
.IR "quot*denominator+rem\ =\ numerator" . |
| 52 |
.P |
| 53 |
The |
| 54 |
.BR ldiv (), |
| 55 |
.BR lldiv (), |
| 56 |
and |
| 57 |
.BR imaxdiv () |
| 58 |
functions do the same, |
| 59 |
dividing numbers of the indicated type and |
| 60 |
returning the result in a structure |
| 61 |
of the indicated name, in all cases with fields |
| 62 |
.I quot |
| 63 |
and |
| 64 |
.I rem |
| 65 |
of the same type as the function arguments. |
| 66 |
.SH RETURN VALUE |
| 67 |
The |
| 68 |
.I div_t |
| 69 |
(etc.) structure. |
| 70 |
.SH ATTRIBUTES |
| 71 |
For an explanation of the terms used in this section, see |
| 72 |
.BR attributes (7). |
| 73 |
.TS |
| 74 |
allbox; |
| 75 |
lbx lb lb |
| 76 |
l l l. |
| 77 |
Interface Attribute Value |
| 78 |
T{ |
| 79 |
.na |
| 80 |
.nh |
| 81 |
.BR div (), |
| 82 |
.BR ldiv (), |
| 83 |
.BR lldiv (), |
| 84 |
.BR imaxdiv () |
| 85 |
T} Thread safety MT-Safe |
| 86 |
.TE |
| 87 |
.SH STANDARDS |
| 88 |
C11, POSIX.1-2008. |
| 89 |
.SH HISTORY |
| 90 |
POSIX.1-2001, C89, C99, SVr4, 4.3BSD. |
| 91 |
.P |
| 92 |
.BR lldiv () |
| 93 |
and |
| 94 |
.BR imaxdiv () |
| 95 |
were added in C99. |
| 96 |
.SH EXAMPLES |
| 97 |
After |
| 98 |
.P |
| 99 |
.in +4n |
| 100 |
.EX |
| 101 |
div_t q = div(\-5, 3); |
| 102 |
.EE |
| 103 |
.in |
| 104 |
.P |
| 105 |
the values |
| 106 |
.I q.quot |
| 107 |
and |
| 108 |
.I q.rem |
| 109 |
are \-1 and \-2, respectively. |
| 110 |
.SH SEE ALSO |
| 111 |
.BR abs (3), |
| 112 |
.BR remainder (3) |