C lldiv() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C lldiv() function computes a long long quotient and remainder. The result contains quot and rem. Division truncates toward zero and the remainder identity holds.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Valid representable division returns both results. No error sentinel exists; invalid division has undefined behavior. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete lldiv() call:

C
lldiv(9000000000LL, 7LL);
CallWhat it does
lldiv(9000000000LL, 7LL);Computes a long long quotient and remainder using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
lldiv_t lldiv(long long numerator, long long denominator);

Parameters

ParameterDescription
numeratorDividend of type long long.
denominatorNonzero divisor of type long long.

Return Value

Return ValueDescription
lldiv_tStructure with long long quot and rem members.

C Examples

Example 1 — Quotient and remainder

This complete program demonstrates a distinct part of the lldiv() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    lldiv_t result = lldiv(17LL, 5LL);
    printf("quot = %lld, rem = %lld\n", result.quot, result.rem);
    return 0;
}

How It Works

  1. Step 1. Supply a nonzero divisor.
  2. Step 2. The function performs one integer division.
  3. Step 3. Read quotient and remainder from the result structure.
  4. Step 4. Verify 17 equals 3 times 5 plus 2.

Example 2 — Negative dividend

This complete program demonstrates a distinct part of the lldiv() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    lldiv_t result = lldiv(-17LL, 5LL);
    printf("%lld %lld\n", result.quot, result.rem);
    return 0;
}

How It Works

  1. Step 1. Use a negative numerator.
  2. Step 2. Division truncates the quotient toward zero.
  3. Step 3. The remainder follows the numerator's sign.
  4. Step 4. Both structure members are printed.

Example 3 — Split into groups

This complete program demonstrates a distinct part of the lldiv() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    lldiv_t boxes = lldiv(53LL, 12LL);
    printf("%lld full, %lld left\n", boxes.quot, boxes.rem);
    return 0;
}

How It Works

  1. Step 1. Treat 53 as the total items.
  2. Step 2. Use 12 as a valid group size.
  3. Step 3. quot counts complete groups.
  4. Step 4. rem counts items left over.

Frequently Asked Questions

lldiv() computes a long long quotient and remainder. The result contains quot and rem. Division truncates toward zero and the remainder identity holds.
Include <stdlib.h> before calling lldiv().
Valid representable division returns both results. No error sentinel exists; invalid division has undefined behavior.
Never use a zero denominator. Avoid LLONG_MIN / -1 because the quotient cannot be represented on common systems.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: malloc()

Continue through the C stdlib.h tutorials.

malloc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful