div(3)
๐ div(3) โ the girlies are dividing integers
okay bestie, welcome to the div(3) page from your Library Functions Manual ๐ this one is short, sweet, and honestly kind of iconic โ it's the "I need my quotient AND my remainder at the same time, in one cute lil struct" page.
โจ NAME โจ
div, ldiv, lldiv, imaxdiv โ compute quotient and remainder of an integer division ๐ like literally both at once, no doing it twice.
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐ซถ
๐ธ SYNOPSIS ๐ธ
#include <stdlib.h>
div_t div(int numerator, int denominator);
ldiv_t ldiv(long numerator, long denominator);
lldiv_t lldiv(long long numerator, long long denominator);
#include <inttypes.h>
imaxdiv_t imaxdiv(intmax_t numerator, intmax_t denominator);
๐ง Feature Test Macro Requirements for glibc ๐ง
(peep feature_test_macros(7) for the whole lore)
lldiv():
_ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
๐ DESCRIPTION ๐
So div() computes the value numerator/denominator and hands you back the quotient AND the remainder, both packed into a structure called div_t that has two integer members โ and their order is unspecified, so don't go assuming, bestie โ named quot and rem. The quotient is rounded toward zero. And the result satisfies this iconic little equation:
quot*denominator+rem = numerator
The ldiv(), lldiv(), and imaxdiv() functions do the exact same thing, just dividing numbers of the indicated type and returning the result in a structure of the indicated name. In every single case, the fields quot and rem have the same type as the function arguments. consistent queen ๐
๐ RETURN VALUE ๐
The div_t (etc.) structure. that's it. that's the whole thing. periodt ๐
๐ฅ ATTRIBUTES ๐ฅ
For an explanation of the terms used in this section, see attributes(7).
div(),ldiv(),lldiv(),imaxdiv()โ Thread safety: MT-Safe โ
โญ STANDARDS โญ
C11, POSIX.1-2008. certified. validated. no cap.
๐ HISTORY ๐
POSIX.1-2001, C89, C99, SVr4, 4.3BSD.
and then โ the glow-up era โ lldiv() and imaxdiv() were added in C99 โจ
๐ EXAMPLES ๐
after this:
div_t q = div(-5, 3);
the values q.quot and q.rem are -1 and -2, respectively. yeah โ negative numbers round toward zero, so -1 and -2, not whatever your vibes were expecting ๐ญ the math said what it said.
๐ฆ SEE ALSO ๐ฆ
ngl bestie if you catch yourself doing/and%right next to each other on the same numbers, just calldiv()instead and save yourself the second division. the struct is right there. take it. ๐ซถ
okay that's the page!! mwah ๐ this was written against Linux man-pages 6.20, dated 2026-02-08, and yes the manual name is div(3) โ go divide something and slay โจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms