C and C++ div_t , ldiv_t and lldiv_t structures


In C and C++ the <cstdlib> or <stdlib.h> header file contain three structures:div_t , ldiv_t and lldiv_t ,which is used as a return type for the functions div , ldiv and lldiv.The structures sole purpose is to act a return type value for the said functions.You can also implement them in your program besides using them just to hold the values returned by the div , ldiv and lldiv functions.

All the structures have two data members.They are given the names ‘quot‘ and ‘rem‘,the names are fixed for all compiler and all of them are integer type.The data member type can be int or long int or long long int type.These classification of the data member type is meant to provide users with various choices for their various needs.If you require a structure for smaller values computation you can used the ldiv_t structure,as the structure data member is int type.For holding larger values you can use lldiv_t,which has long long int as the data member type.

The definition of the three structures are given below.The first definition is for ‘div_t’ structure.

Code example:The first definition is for div_t structure

struct div_t {
  int quot;
  int rem;
};

Code example:The second definition is for ldiv_t structure

struct ldiv_t {
  long int quot;
  long int rem;
};

Code example:The third definition is for lldiv_t structure

struct div_t {
  long long int quot;
  long long int rem;
};

To show how these structures are used or can be used in a program an example is provided below.

Code example :For C++ programmer

#include <iostream>
#include <cstdlib>

using namespace std ;

int main( )
{
//used in ‘div’ function
div_t d ;

d=div( 23 , 5 );

cout<< “Remainder of ’23/5′ =” << d.rem ///give the remainder of 23/5
<< “\nQuotient of ’23/5′ =” << d.quot ; ///gives the quotient of 23/5

cout<< “\n\n” ;
///use for other purpose
lldiv_t lld ;

lld.rem=45%4 ; ///the remainder of 45 divided by 4 is stored in lld.rem
lld.quot=(45-lld.rem)/4 ; ///quotient of 45/4 is stored in lld.quot

cout<< lld.rem << endl
<< lld.quot << endl ;

cin.get( );
return 0;
}

Output

Remainder of ’23/5′ =3
Quotient of ’23/5′ =4

1
11

For more information on what ‘div’,’ldiv’ and ‘lldiv’ function do visit the link provided below.

Link C++ cstdlib div , ldiv and lldiv functions

Code example :For C programmer

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

int main( )
{
//used in ‘div’ function
div_t d ;

d=div( 23 , 5 );

printf( “Remainder of ’23/5′ =%i” , d.rem )///give the remainder of 23/5
printf( “\nQuotient of ’23/5′ =%i” , d.quot) ; ///gives the quotient of 23/5

printf( “\n\n” );
///use for other purpose
lldiv_t lld ;

lld.rem=45%4 ; ///the remainder of 45 divided by 4 is stored in lld.rem
lld.quot=(45-lld.rem)/4 ; ///quotient of 45/4 is stored in lld.quot

printf( “%lli \n%lli”, lld.rem , lld.quot) ;

getchar( );
return 0;
}

Output

Remainder of ’23/5′ =3
Quotient of ’23/5′ =4

1
11

More about C div,ldiv and lldiv here C programming div,ldiv,lldiv stdlib.h.