Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

lldiv — C23 arithmetic utility function

`lldiv(numer, denom)` from `<stdlib.h>` computes a `long long int` quotient and remainder and returns an `lldiv_t` with `quot` and `rem`. Division truncates toward zero, and `numer == quot * denom + rem`.

Version: C23

Syntax

lldiv_t lldiv(long long int numer, long long int denom);

lldiv(numer, denom) from <stdlib.h> computes a long long int quotient and remainder and returns an lldiv_t with quot and rem. Division truncates toward zero, and numer == quot * denom + rem.

The example calls lldiv(-1000000000001LL, 10LL) and prints -100000000000 -1 -1000000000001, demonstrating the remainder sign and reconstruction of the numerator.

The denominator must be nonzero. If the quotient is not representable as long long, notably LLONG_MIN / -1LL, behavior is undefined. Also guard your own reconstruction multiplication against overflow; use div for int and ldiv for long.

Examples

#include <stdio.h>
#include <stdlib.h>
int main(void){lldiv_t r=lldiv(-1000000000001LL,10LL);printf("%lld %lld %lld\n",r.quot,r.rem,r.quot*10LL+r.rem);return 0;}

Result

-100000000000 -1 -1000000000001

The example calls `lldiv(-1000000000001LL, 10LL)` and prints `-100000000000 -1 -1000000000001`, demonstrating the remainder sign and reconstruction of the numerator.

Sources