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 ๐Ÿฆ‹

abs(3), remainder(3)


ngl bestie if you catch yourself doing / and % right next to each other on the same numbers, just call div() 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms