C Standard Library

C <stdlib.h> - ldiv() Function



The C <stdlib.h> ldiv() function computes both the quotient and the remainder of the division of the integral numerator numer by the integral denominator denom.

Syntax

ldiv_t ldiv (long int numer, long int denom);

Parameters

numer Specify an integral value for numerator.
denom Specify an integral value for denominator.

Return Value

Returns both the remainder and the quotient can be represented as objects of type ldiv_t defined as follows:

struct ldiv_t { long quot; long rem; };

or 

struct ldiv_t { long rem; long quot; };

Example:

The example below shows the usage of <stdlib.h> ldiv() function.

#include <stdio.h>
#include <stdlib.h>
 
int main (){
  ldiv_t result = ldiv(50, 17);

  printf("ldiv(50, 17) gives quotient = %ld and remainder = %ld\n",
         result.quot, result.rem);
  
  return 0;
}

The output of the above code will be:

ldiv(50, 17) gives quotient = 2 and remainder = 16

❮ C <stdlib.h> Library