w3resource

C ldiv() function

C ldiv() function - Long and Long Long Division

Syntax ldiv() function

div_t div(long int numer, long int denom)

The C library function div_t div(long int numer, long int denom) divides numer (numerator) by denom (denominator).

The ldiv() function is used to calculate the quotient and remainder of the division of numerator by denominator.

Parameters ldiv() function

Name Description Required /Optional
numer This is the numerator. Required
denom This is the denominator. Required

Return value from ldiv()

  • A structure of type ldiv_t, containing both the quotient (long int quot) and the remainder (long int rem).
  • If the value cannot be represented, the return value is undefined.
  • If denominator is 0, an exception is raised.

Example: ldiv() function

The following example shows the usage of ldiv() function.


#include <stdlib.h>
#include <stdio.h>
 
int main(void)
 {
   long int nums[4] = {7,76,-45,-34};
   long int den[4] = {3,7,5,-7};
   ldiv_t ans;   /* ldiv_t is a struct type containing two long ints:
                    'quot' stores quotient; 'rem' stores remainder */
   short i,j;
 
   printf("Results of long division:\n\n");
   for (i = 0; i < 4; i++)
      for (j = 0; j < 4; j++)
      {
         ans = ldiv(nums[i], den[j]);
         printf("Dividend: %6ld  Divisor: %6ld", nums[i], den[j]);
         printf("  Quotient: %6ld  Remainder: %6ld\n", ans.quot, ans.rem);
      }
}

Output:

Results of long division:

Dividend:      7  Divisor:      3  Quotient:      2  Remainder:      1
Dividend:      7  Divisor:      7  Quotient:      1  Remainder:      0
Dividend:      7  Divisor:      5  Quotient:      1  Remainder:      2
Dividend:      7  Divisor:     -7  Quotient:     -1  Remainder:      0
Dividend:     76  Divisor:      3  Quotient:     25  Remainder:      1
Dividend:     76  Divisor:      7  Quotient:     10  Remainder:      6
Dividend:     76  Divisor:      5  Quotient:     15  Remainder:      1
Dividend:     76  Divisor:     -7  Quotient:    -10  Remainder:      6
Dividend:    -45  Divisor:      3  Quotient:    -15  Remainder:      0
Dividend:    -45  Divisor:      7  Quotient:     -6  Remainder:     -3
Dividend:    -45  Divisor:      5  Quotient:     -9  Remainder:      0
Dividend:    -45  Divisor:     -7  Quotient:      6  Remainder:     -3
Dividend:    -34  Divisor:      3  Quotient:    -11  Remainder:     -1
Dividend:    -34  Divisor:      7  Quotient:     -4  Remainder:     -6
Dividend:    -34  Divisor:      5  Quotient:     -6  Remainder:     -4
Dividend:    -34  Divisor:     -7  Quotient:      4  Remainder:     -6

C Programming Code Editor:

Previous C Programming: C labs()
Next C Programming: C rand()



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
{
    ptr = 0;// oops, I meant *ptr = 0
}

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q