The div, ldiv, and lldiv, functions compute quotient (numer / denom ) and remainder (numer % denom ) in a single operation.
These functions div, ldiv, and lldiv, are defined in stdlib.h header file and returns a structure that comprises the quotient and the remainder. So let see these function one by one with some example code.
Note: If either the remainder or the quotient cannot be represented, the behavior is undefined.
div():
The div function takes an int as an argument and computes quotient and remainder in a single operation. The first parameter passed in a div() function is taken as the numerator and the 2nd parameter is taken as the denominator. Let’s see the syntax of the div function.
//Syntax of div function div_t div(int numer, int denom);
Parameters:
numer— The numerator.
denom— The denominator.
Return:
The div function returns a structure of type div_t, comprising both the quotient and the remainder. The div_t structure looks like this:
typedef struct { int quot; /* Quotient. */ int rem; /* Remainder. */ } div_t;
Let’s see an example code to understand the functionality of the div in C. This C code, calculate the quotient and remainder of two given numbers by calling div functions. Finally, it prints the structure members quot and rem.
#include <stdio.h> #include<stdlib.h> int main() { div_t div_result; div_result = div(25,6); printf("Quotient = %d\n", div_result.quot); printf("Remainder = %d\n", div_result.rem); return 0; }
When you run the program, the output will be:
ldiv():
The ldiv function takes an long int as an argument and computes quotient and remainder in a single operation. The first parameter passed in a ldiv() function is taken as the numerator and the 2nd parameter is taken as the denominator. Let’s see the syntax of the ldiv function.
//Syntax of ldiv function ldiv_t ldiv(long int numer, long int denom);
Parameters:
numer— The numerator.
denom— The denominator.
Return:
The ldiv function returns a structure of type ldiv_t, comprising both the quotient and the remainder. The ldiv_t structure looks like this:
typedef struct { long int quot; /* Quotient. */ long int rem; /* Remainder. */ } ldiv_t;
Let’s see an example code to understand the functionality of the ldiv in C. This C code, calculate the quotient and remainder of two given numbers by calling ldiv functions. Finally, it prints the structure members quot and rem.
#include <stdio.h> #include<stdlib.h> int main() { ldiv_t ldiv_result; ldiv_result = ldiv(17267012L,60L); printf("Quotient = %ld\n", ldiv_result.quot); printf("Remainder = %ld\n", ldiv_result.rem); return 0; }
When you run the program, the output will be:
Quotient = 287783 Remainder = 32
Note: for long int, you should use %ld format specifier with printf. If you are new in C programming you can read the article “Format specifiers in C“.
lldiv():
The lldiv function takes an long long int as an argument and computes quotient and remainder in a single operation. The first parameter passed in a lldiv() function is taken as the numerator and the 2nd parameter is taken as the denominator. Let’s see the syntax of the lldiv function.
//Syntax of lldiv function lldiv_t lldiv(long long int numer, long long int denom);
Parameters:
numer— The numerator.
denom— The denominator.
Return:
The lldiv function returns a structure of type lldiv_t, comprising both the quotient and the remainder. The lldiv_t structure looks like this:
typedef struct { long long int quot; /* Quotient. */ long long int rem; /* Remainder. */ } lldiv_t;
Let’s see an example code to understand the functionality of the lldiv in C. This C code, calculate the quotient and remainder of two given numbers by calling lldiv functions. Finally, it prints the structure members quot and rem.
#include <stdio.h> #include<stdlib.h> int main() { lldiv_t lldiv_result; lldiv_result = lldiv(172670128L,90L); printf("Quotient = %lld\n", lldiv_result.quot); printf("Remainder = %lld\n", lldiv_result.rem); return 0; }
When you run the program, the output will be:
Quotient = 1918556 Remainder = 88
Recommended Articles for you:
- abs labs llabs functions in C/C++
- Best Gifts for the programmer and techies.
- How to use and implement your own strcat in C.
- How to implement atoi in C/C++;
- Use and create strspn in programming.
- How to make memcpy function in C
- memmove vs memcpy.
- Implement vector in C.
- How to Use strncpy() and implement own strncpy().
- How to pass an array as a parameter?
- 10 Best C Programming Books.
- Best mouse for a programmer.
- Memory Layout in C.