liveBook · Manning

Div

As explained in Modern C, Third Edition

div is a C standard-library function that computes the quotient and remainder of two int values and returns them in a structure.

div computes integer quotient and remainder together, returning both values in a structure from <stdlib.h>. Access them through its quot and rem members. section 8.2

Capture both results

auto res = div(x, y); printf("%d/%d is %d, remainder %d\n", x, y, res.quot, res.rem);

TAKEAWAY:

  • Use div when one operation should provide both the quotient and remainder. section 8.2
  • Avoid a zero divisor and combinations such as INT_MIN cases that violate division constraints. section 8.2
Table 8.5 Functions for integer arithmetic. Most of these are type-generic macros, but some also have function interfaces for specific types. \(x\) and \(y\) denote the arguments, \(\overline{x}\) is a bitwise complement, \(w\) the width, and \(m\) the maximum value of the type.
Function Description Remark
abs, labs, llabs \(|x|\) Type independent
div, ldiv, lldiv \(x\)/\(y\) and \(x\)%\(y\) Type independent
ckd_add \(x\)+\(y\) (LS bits) & overflow flag Target dependent
ckd_mul \(x\)*\(y\) (LS bits) & overflow flag Target dependent
ckd_sub \(x\)-\(y\) (LS bits) & overflow flag Target dependent
stdc_bit_ceil \(2^{\lceil \log_2 x\rceil}\) \(1\) if \(x \equiv 0\), \(0\) if \(x > m/2\)
stdc_bit_floor \(2^{\lfloor \log_2 x\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_bit_width \(1 + {\lfloor \log_2 x\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_count_ones Number of \(1\)-bits in \(x\) Type independent
stdc_count_zeros Number of \(1\)-bits in \(\overline{x}\) Type dependent
stdc_has_single_bit There is 𝑁 with \(x \equiv 2^n\) Type independent
stdc_first_leading_one \(w - {\big\lfloor \log_2 x\big\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_first_leading_zero \(w - {\big\lfloor \log_2 \overline{x}\big\rfloor}\) \(0\) if \(x\) has all bits set
stdc_leading_ones \(w - {\big\lfloor \log_2 \overline{x}\big\rfloor} - 1\) \(w\) if \(x \equiv 0\)
stdc_leading_zeros \(w - {\big\lfloor \log_2 x\big\rfloor} - 1\) \(w\) if \(x \equiv 0\)
stdc_first_trailing_one One plus LS \(1\)-bit in \(x\) \(0\) if \(x \equiv 0\)
stdc_first_trailing_zero One plus LS \(0\)-bit in \(x\) \(0\) if \(x \equiv 0\)
stdc_trailing_ones One plus LS \(0\)-bit in \(x\) \(w\) if \(x \equiv 0\)
stdc_trailing_zeros One plus LS \(1\)-bit in \(x\) \(w\) if \(x \equiv 0\)

Compare div with related integer-arithmetic interfaces, including long and long long variants. section 8.2

How Modern C, Third Edition covers it

How deep it goes
Defines returned structure members and validity exceptions, but defers detailed structure naming and implementation mechanics.
Examples
Uses a C23 auto declaration, printf output, and quot and rem member access.
What it assumes
Assumes integer division, remainder, structures, and <stdlib.h>; clarifies the returned members rather than teaching these from zero.
What you can do after
Apply it via div to obtain quotient and remainder values from one integer-arithmetic call.
How it connects
Connects integer arithmetic with quotient, remainder, structures, <stdlib.h>, and the div, ldiv, and lldiv interfaces.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.