ldiv C Library Function

The function ldiv divides numerator by denominator and returns the quotient and remainder of the division. It returns a structure of type ldiv_t, which has two members: quot(quotient) and rem(remainder).

Function prototype of ldiv

ldiv_t ldiv(long int numerator, long int denominator);
  • numerator : This is a long integer representing the numerator.
  • denominator : This is a long integer representing the denominator.

Return value of ldiv

This function returns the quotient and remainder of numerator/denominator as a ldiv_t type(defined in stdlib library) which has two members quot(quotient) and rem(remainder).

C program using ldiv function

The following program shows the use of ldiv function to find quotient and remainder of long integer division.

ldiv C Library Function

#include <stdio.h>
#include <stdlib.h>
int main(){
    long int numerator, denominator;
    ldiv_t response;
    printf("Enter numerator and denominator\n");
    scanf("%ld %ld", &numerator, &denominator);
    response = ldiv(numerator, denominator);
    printf("Remainder of %ld/%ld is %ld\n", 
        numerator, denominator, response.rem);
    printf("Quotient of %ld/%ld is %ld\n", 
        numerator, denominator, response.quot);
    return 0;


Enter numerator and denominator
826847 246
Remainder of 826847/246 is 41
Quotient of 826847/246 is 3361