C++ Standard Library C++ STL Library

C++ <cstdlib> - ldiv_t structure type



The C++ <cstdlib> ldiv_t is a structure to represent both the quotient and the remainder of the division of the integral numerator by integral denominator. This is the type returned by ldiv() function.

In the <cstdlib> header file, it is 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 <cstdlib> ldiv_t type.

#include <iostream>
#include <cstdlib>
using namespace std;
 
int main (){
  ldiv_t result = ldiv(50, 17);

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

The output of the above code will be:

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

❮ C++ <cstdlib> Library