C div() function
C div() function - Calculate quotient and remainder
The div() function is used to calculate the quotient and remainder of the division of numerator by denominator.
Syntax div() function
div_t div(int numer, int denom)
Parameters div() function
Name | Description | Required /Optional |
---|---|---|
numer | The numerator. | Required |
denom | The denominator. | Required |
Return value from div() function
- A structure of type div_t, containing both the quotient int quot and the remainder integer rem.
- If the return value cannot be represented, its value is undefined.
- If denominator is 0, an exception will be raised.
Example: div() function
The following example shows the usage of div() function.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int nums[4] = {6,56,-34,-45};
int den[4] = {5,7,3,-7};
div_t ans; /* div_t is a struct type contain two ints:
'quot' stores quotient; 'rem' stores remainder*/
short i,j;
printf("Results of division:\n");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
ans = div(nums[i],den[j]);
printf("Dividend: %6d Divisor: %6d", nums[i], den[j]);
printf(" Quotient: %6d Remainder: %6d\n", ans.quot, ans.rem);
}
}
Output:
Results of division: Dividend: 6 Divisor: 5 Quotient: 1 Remainder: 1 Dividend: 6 Divisor: 7 Quotient: 0 Remainder: 6 Dividend: 6 Divisor: 3 Quotient: 2 Remainder: 0 Dividend: 6 Divisor: -7 Quotient: 0 Remainder: 6 Dividend: 56 Divisor: 5 Quotient: 11 Remainder: 1 Dividend: 56 Divisor: 7 Quotient: 8 Remainder: 0 Dividend: 56 Divisor: 3 Quotient: 18 Remainder: 2 Dividend: 56 Divisor: -7 Quotient: -8 Remainder: 0 Dividend: -34 Divisor: 5 Quotient: -6 Remainder: -4 Dividend: -34 Divisor: 7 Quotient: -4 Remainder: -6 Dividend: -34 Divisor: 3 Quotient: -11 Remainder: -1 Dividend: -34 Divisor: -7 Quotient: 4 Remainder: -6 Dividend: -45 Divisor: 5 Quotient: -9 Remainder: 0 Dividend: -45 Divisor: 7 Quotient: -6 Remainder: -3 Dividend: -45 Divisor: 3 Quotient: -15 Remainder: 0 Dividend: -45 Divisor: -7 Quotient: 6 Remainder: -3
C Programming Code Editor:
Previous C Programming: C abs()
Next C Programming: C labs()
C Programming: Tips of the Day
Returning an array using C
You can't return arrays from functions in C. You also can't (shouldn't) do this:
char *returnArray(char array []){ char returned [10]; //methods to pull values from array, interpret them, and then create new array return &(returned[0]); //is this correct? }
returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.
You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.
Dynamically allocate the memory inside of the function (caller responsible for deallocating ret)
char *foo(int count) { char *ret = malloc(count); if(!ret) return NULL; for(int i = 0; i < count; ++i) ret[i] = i; return ret; }
Call it like so:
int main() { char *p = foo(10); if(p) { // do stuff with p free(p); } return 0; }
Ref : https://bit.ly/3yFIeao
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook